-1

I also tried in mongoose doc and MongoDB community but I couldn't find any solution.

How can i query document by populated document?

1 Answers1

1

You should use an aggregation pipeline in order to achieve this. The following code will perform the lookup (same as populate) first and then match on the populated fields:

const story = await Story.aggregate([
  {
    "$lookup": {
      "from": "authors",
      "localField": "author",
      "foreignField": "_id",
      "as": "author"
    }
  },
  {
    $match: {
      "author.name": "Ian Fleming"
    }
  }
])
Fabian Strathaus
  • 3,192
  • 1
  • 4
  • 26