2021-09-17

MongoDB - Lookup using multiple custom matching criteria

db.MAIN_COLLECTION_NAME.aggregate([

    {$match: {

        "ANY_FIELD_0": "ANY_VALUE",

    }},


    {$lookup: {

        from: "COLLECTION_NAME_TO_LOOKUP_FROM",

        let: {

            "FIELD_ALIAS_1": "$MAIN_COLLECTION_ANY_FIELD_1",

            "FIELD_ALIAS_2": "$MAIN_COLLECTION_ANY_FIELD_2",

        },

        pipeline: [

            {$match: {

                $expr: {$eq: ["$ANY_FIELD_1", "$$FIELD_ALIAS_1"]},

                $expr: {$eq: ["$ANY_FIELD_2", "$$FIELD_ALIAS_2"]},

            }},

        ],

        as: "LOOKUP_RESULTS_FIELD_ALIAS"

    }},

]);

No comments:

Post a Comment