2022-04-08

MongoDB - Match based on multiple keys of an array of objects

 db.collection_name.aggregate([

    {$match: {

        "array_of_objects": {

            $elemMatch: {

                "object_property_1": value_1,

                "object_property_2": value_2

            }

        }

    }},

]);


MongoDB - Match based on multiple keys of an array of objects NOT having the given values

 db.collection_name.aggregate([

    {$match: {

        $nor: [

            {"array_of_objects": {

                $elemMatch: {

                    "object_property_1": value_1,

                    "object_property_2": value_2

                }

            }}

        ]

    }},

]);

2022-04-02

MongoDB - Filter array

 db.collection_name.aggregate([

    {$addFields: {

        list: {$filter: {

            input: '$array_field_name',

            as: 'item',

            cond: {

                $gt: [

                    '$$item.array_subfield_name', 

                    value

                ]

            }

        }}

    }}

]);