如何查找ID在数组中的集合中的项目?

时间:2016-11-02 10:33:31

标签: node.js mongodb sails.js

我有一个名为Vendor的集合,我想找到ID在ID列表中的所有供应商。有没有办法在Sails.js中执行此操作,或者我必须遍历列表以查找其ID与列表中的一个项目匹配的那些。

我在Sails中使用MongoDB。

2 个答案:

答案 0 :(得分:2)

您可以在 $in 方法中使用 .native() 运算符,如下所示

Vendor.native(function(err, collection) {
    if (err) return console.log(err);
    collection.find({ "_id": { "$in": ids } }).toArray(function(err, results) {
        console.log(results);
    });
});

答案 1 :(得分:1)

正如您在https://mahout.apache.org/users/recommender/userbased-5-minutes.html#dataset中看到的那样:

Vendor.find({
  id: ids // ids is Array 
})
.then(vendors => {...})
.catch(err => {...});