Mongoose populate where Mongoose populate where mongoose mongoose

Mongoose populate where


I don't think there is any way to do what you're trying to do. The code sample in your question doesn't work as you are expecting because M1 has no knowledge of m2.title. M1 only knows about its array of M2 ObjectId's.

You're going to have to query for all M1 documents that match your M1 criteria and populate M2:

const results = await M1.find({  // ... m1 criteria}).populate('m2').exec()

Then filter out all of the results that match your m2 title afterwards:

const finalResults = results.filter(({ m2 }) => m2.title === 'title-match')