Skip to content Skip to sidebar Skip to footer

How To Include Only Selected Embedded Document In Mongoengine?

I'm fetching documents on the base of specific EmbeddedDocument, but I don't want to get all of the EmbeddedDocuments while retrieving, only the matched EmbeddedDocument with main

Solution 1:

You can use the $filter operator in the aggregate function as

users = User.objects(school__match={ "name": "Aukamm Elementary School" }).aggregate(
    { "$project": {
        "first_name": 1,
        "last_name": 1,
        "schools": {
             "$filter": {
                  "input": "$schools",
                  "as": "school",
                  "cond": { "$eq": [ "$$school.name", "Aukamm Elementary School" ] }
             }
        }
    } }
)

Post a Comment for "How To Include Only Selected Embedded Document In Mongoengine?"