简体   繁体   English

node.js使用和尚从数组中获取ID的文档

[英]node.js getting documents by id from an array using monk

I'm trying to fetch a few documents from a collection in mongodb with a set of ids 我正在尝试从mongodb的集合中获取一些带有一组ID的文档

similar to SQLish 类似于SQLish

SELECT * FROM TABLE WHERE <id> IN [1,3,4]

Here is the code i'm using: 这是我正在使用的代码:

var prodCollection = req.db.get('products');
      prodCollection.find({'-_id':{'$in':["572446507cd5bb7304d75993","572446507cd5bb7304d75994"]}},{}, function(err, docs){
        if(err) throw err;
        res.send(docs);
      });

my setup is a node.js server running on local host using express and monk. 我的设置是使用express和尚在本地主机上运行的node.js服务器。 i've also tried various versions of the same code with or without single quotes for the IN and _id parameters but with no luck, my returned docs array is always empty. 我也尝试过使用同一代码的不同版本,无论是否使用IN和_id参数都使用单引号,但是没有运气,我返回的docs数组始终为空。 I can confirm my database indeed contain the collection and the docs by accessing it from the console : 我可以通过从控制台访问数据库来确认我的数据库确实包含集合和文档:

> db.products.find()
{ "_id" : ObjectId("572446507cd5bb7304d75993"), "name" : "apples", "quantity" : 20, "checked" : false }
{ "_id" : ObjectId("5724466c7cd5bb7304d75994"), "name" : "bananas", "quantity" : 2, "checked" : false }
{ "_id" : ObjectId("5724467e7cd5bb7304d75995"), "name" : "cerial", "quantity" : 10, "checked" : true }

You are trying to query database using string, where ObjectID is an actual object. 您正在尝试使用字符串查询数据库,其中ObjectID是实际对象。 To convert string to ObjectID you can use mongoose.Types.ObjectId(string) , I am not sure that this will fix your problem, but it is a step in the right direction. 要将string转换为ObjectID,可以使用mongoose.Types.ObjectId(string) ,我不确定这是否可以解决您的问题,但这是朝正确方向迈出的一步。

It seems like both MaxKroshka and Tal Avissar are correct, i managed to make the query work (though for now it still only return only the first item by modifying my code as suggested: 看来MaxKroshka和Tal Avissar都是正确的,我设法使查询正常工作(尽管目前它仍然仅通过修改建议的代码仅返回第一项:

first of all it is true that monk does not require the _id field name to be escaped with the - sign so i replaced it with a normal _id string. 首先,和尚不需要_id字段名称以-符号进行转义是对的,因此我用普通的_id字符串替换了它。

secondly it seems that it is possible to convert from a plain hex id string into a fully fledged id object that can be passed to the query using monks 'collection.id(hexString)' function. 其次,似乎可以将纯正的十六进制id字符串转换为完全成熟的id对象,可以使用僧侣的'collection.id(hexString)'函数将其传递给查询。

here is my modified code which worked (again, but only returning the first one of the provided array) 这是我修改后的代码,可以正常工作(再次,但仅返回提供的数组的第一个)

var prodCollection = req.db.get('products');
var idObjects = [];

//this was the real magic to get it working, converting string id to object
for(var i=0; i<group.products.length; i++){
    dObjects.push(prodCollection.id(group.products[i]));
}

prodCollection.find({'_id':{$in:idObjects}},{}, function(err, docs){
if(err) throw err;
    res.send(docs);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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