0

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) => {
  ....
});

2 Answers 2

1

use $in and match in populate query

Collection.findOne({element: elementId})
.populate({
  path: 'references',
  match: {
    _id: {
      $in: ["5ae9b6ac268d162b15747340", "5ae9b6ac268d162b15747341"]
    }
  }
})
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.