简体   繁体   中英

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. But the problem is that I am passing id in the form of string and want to match it with objectId of the document. 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"};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM