[英]How can I find document by objectId and one more field in mongodb using nodejs?
I want to find a document by ObjectId and one more field in mongodb using nodejs. 我想使用Node.js通过ObjectId和mongodb中的另一个字段查找文档。 But the problem is that I am passing id in the form of string and want to match it with objectId of the document.
但是问题是我正在以字符串形式传递id,并希望将其与文档的objectId匹配。 Help me
帮我
module.exports.getProduct = function(id, callback){
//here id is "59f5v26sdf4grgbawf"
var query = {_id : id , status : "approved"};
//and _id is in the form of ObjectId("59f5v26sdf4grgbawf")
Product.find(query, callback)
}
You need to convert a string to Object. 您需要将字符串转换为对象。
If you are using Mongoose, then it will looks like that: 如果您使用的是猫鼬,它将看起来像这样:
var query = {_id : mongoose.Types.ObjectId(id) , status : "approved"};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.