filter nested in populate

Viewed 80

Good morning everyone I have a problem with the populate issue in mongoose especially with the filters issue, I will explain what I want to do:

this is my current code:

      let consult = await Experiences.find({}).populate("comments", {
    experienceId: 0,
    __v: 0
  }).populate({
    path: "city",
    __v: 0,
    match: { 'name': {$in: req.query.city}},
    select: ['name','state']
  });

what I need here is to filter by city the total results not only of the city field in the populate, since it returns me the 2 data that I have and the city of which does not meet the condition in null I need is not to show it directly.

it returns:

{
"success": true,
"consult": [
    {
        "comments": [],
        "_id": "616a96b05550a5f9959e724a",
        "name": "Museo",
        "city": null,
        "date": "2021-10-16T09:09:04.495Z",
        "__v": 1,
        "rate": 5
    },
    {
        "comments": [],
        "_id": "616a9dbeebc9e9ff2c95431a",
        "name": "Museo  nuevo",
        "city": {
            "_id": "616a9da2ebc9e9ff2c954317",
            "name": "Miami"
        },
        "date": "2021-10-16T09:39:10.420Z",
        "__v": 0
    }
]
}

i need this:

{
"success": true,
"consult": [
    {
        "comments": [],
        "_id": "616a9dbeebc9e9ff2c95431a",
        "name": "Museo  nuevo",
        "city": {
            "_id": "616a9da2ebc9e9ff2c954317",
            "name": "Miami"
        },
        "date": "2021-10-16T09:39:10.420Z",
        "__v": 0
    }
]
}
0 Answers
Related