简体   繁体   English

我如何使用nodejs在mongodb中按objectId和一个字段查找文档?

[英]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.

相关问题 find() 如何访问文档字段以进行操作? (MongoDB,nodeJS) - How can a find() access a document field for an operation? (MongoDB, nodeJS) 如何使用nodejs在mongodb中按自定义字段名称(不是_id)查找文档? - How to find the document by custom field name (not _id) in the mongodb using nodejs? 如果文档使用mongodb和nodejs存在于集合中,那么如何更新一个字段? - How to update one field if the document exist in a collection using mongodb and nodejs? 如何通过使用 nodejs 在 mongodb 中指定其匹配字段之一来修改记录的字段 - How can i modify a field of a record by specifying one of its matching field in mongodb using nodejs 如何从 mongoDB 获取多个文档? - How can I get more than one document from mongoDB? 如何使用过滤器 _id 找到一条记录并使用 nodejs 和 mongodb,mongoose 更新该记录 - how can i find one record with the filter _id and update that record using the nodejs and mongodb,mongoose 如何获取嵌入式 MongoDB 文档的某个字段,知道其 ObjectId - How to get a certain field of an embedded MongoDB document knowing its ObjectId 如何使用nodejs在mongodb中的每个文档的字段中添加数字 - how to add number to field in every document in mongodb using nodejs MongoDB:按数组中的一个objectId查找 - MongoDB: find by one objectId in array 我们如何使用 mongoose 和 expressjs-nodejs 对 mongodb 中的 ObjectId 列执行排序? - How we can perform sort on ObjectId column in mongodb using mongoose with expressjs-nodejs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM