Valip
Valip

Reputation: 4620

Mongoose using populate with an array of id's

Is there any way to use populate in order to get just the documents with the id's specified in an array?

For example, this is how it works right now

Collection.findOne({element: elementId})
.populate({
  path: references
})
.exec((err, run) => {
  ....
});

And I'm searching for something like:

Collection.findOne({element: elementId})
.populate({
  path: {references: {_id: ["5ae9b6ac268d162b15747340", "5ae9b6ac268d162b15747341"]}}
})
.exec((err, run) => {
  ....
});

Upvotes: 0

Views: 40

Answers (2)

Ashh
Ashh

Reputation: 46451

use $in and match in populate query

Collection.findOne({element: elementId})
.populate({
  path: 'references',
  match: {
    _id: {
      $in: ["5ae9b6ac268d162b15747340", "5ae9b6ac268d162b15747341"]
    }
  }
})

Upvotes: 1

Nick
Nick

Reputation: 352

Yes, you would need to first declare it in your mongoose schema so it knows where to populate the data from. Please refer to : http://mongoosejs.com/docs/populate.html

Upvotes: 0

Related Questions