node.js+express + mongodb,通过ID查询数据失败
我在这里通过mongodb的ID来查询数据
我的步骤:
1、引包var ObjectId = require('mongodb').ObjectId;
2、ObjectId(_id)
3、查询
但是我ObjectId(_id)包装后的ID的值就改变了,不是包装的前的ID了,查询的结果也为空
var ObjectId = require('mongodb').ObjectId;exports.getArticleById = function (req,res,next) { var id= req.query.id; var _id = ObjectId(_id); console.log(id); console.log(typeof(id)); console.log("包装后的ID:"+_id); console.log(typeof(_id)); db.find("article", {"id": _id}, function (err, result) { if (err) { res.send("文章被删除"); return } console.log(result); }); res.render('article/article', { "login": req.session.login == "1" ? true : false, "username": req.session.login == "1" ? req.session.username : "" });};这是结果:
有毒,这里哪里用错了吗?
然后我换mongoose来转就成功了,数据也拿到了
var mongoose = require('mongoose');var _id = mongoose.Types.ObjectId(id); |
免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。
版权声明:作者保留权利,不代表本站立场。
|
|
|
|
|
|
|
|
有没有试过不用 ObjectId 包装 id,不懂为何要包装一下子,直接查询不行吗 |
|
|
|
|
|
|
|