'Query by populated model attribute

I have the following data example queried from mongoose:

  {
    _id: new ObjectId("622a0cbef19bd38844b8755d"),
    creator: new ObjectId("61e955a77aece40e1e5b5535"),
    description: 'XYZ',
    category: { _id: new ObjectId("6226704b9410d37a7686224b"), public: true },
    active: true,
  }

I would like to get all documents where category.public equals true

In my controller I tried the following query:

    const task = await Task.find({
      creator: userid,
      active: true,
    }).populate({ path: 'category', model: Category, select: 'public' })
      .where('category.public').equals(true)
      .sort({
        active: 'desc',
      });

But the result is always empty. I know that there are some matching documents and if I delete .where('category.public').equals(true) it returns all records. Any ideas how to query with a populated attribute? There's probably something wrong with my approach.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source