user3070752
user3070752

Reputation: 734

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.

Upvotes: 0

Views: 42

Answers (2)

SkyQ
SkyQ

Reputation: 380

As you can see in doc:

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

Upvotes: 1

chridam
chridam

Reputation: 103355

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);
    });
});

Upvotes: 2

Related Questions