user3070752 user3070752 - 1 month ago 14
Node.js Question

How to find items in a collection whose IDs are in an array?

I have a collection named

Vendor
and I want to find all vendors whose IDs are in a list of IDs. Is there any way to do it in Sails.js or I must iterate over the list to find those whose IDs match with one item in the list.

I'm using MongoDB in Sails.

Answer

You can use the $in operator within the .native() method as follows

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