我在这里通过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);
添加回答
举报
0/150
提交
取消