чтобы запустить преобразование, попробуйте:

etails

{
    "_id" : "5c23536f807caa1bec00e79b",
    "UID" : "1",
    "name" : "A",
},
{
    "_id" : "5c23536f807caa1bec00e78b",
    "UID" : "2",
    "name" : "B",
},
{
"_id" : "5c23536f807caa1bec00e90",
"UID" : "3",
"name" : "C"
}

UserProducts

{
    "_id" : "5c23536f807caa1bec00e79c",
    "UPID" : "100",
    "UID" : "1",
    "status" : "A"
},
{
    "_id" : "5c23536f807caa1bec00e79c",
    "UPID" : "200",
    "UID" : "2",
    "status" : "A"
},
{
"_id" : "5c23536f807caa1bec00e52c",
"UPID" : "300",
"UID" : "3",
"status" : "A"
}

группы

{
    "_id" : "5bb20d7556db6915846da55f",
    "members" : {
        "regularStudent" : [
            "200" // UPID
        ],
    }
},
{
"_id" : "5bb20d7556db69158468878",
"members" : {
    "regularStudent" : {
        "0" : "100" // UPID
    }
}
}

Шаг 1

Я должен взять UID отUserDetails проверить сUserProducts затем взятьUPID изUserProducts

Шаг 2

мы должны это проверитьUPID сопоставлены сгруппы коллекция или нет?members.regularStudent мы сопоставленыUPID

Шаг 3

предполагатьUPID не отображается означает, что я хочу напечататьUPID отUserProducts

Я пытался, но не смог закончить, пожалуйста, помогите мне в этом.

Ожидаемый результат:

["300"]

Примечание: ожидаемый результат["300"] , потому чтоUserProducts имеющий UPID100 & 200 ногруппы коллекция отображается только100& 200.

Мой код

var queryResult = db.UserDetails.aggregate(
{
$lookup: {
    from: "UserProducts",
    localField: "UID",
    foreignField: "UID",
    as: "userProduct"
    }
},
{ $unwind: "$userProduct" },
{ "$match": { "userProduct.status": "A" } },
{
    "$project": { "_id" : 0, "userProduct.UPID" : 1 }
},
{
    $group: {
        _id: null,
        userProductUPIDs: { $addToSet: "$userProduct.UPID" }
    }
});

let userProductUPIDs = queryResult.toArray()[0].userProductUPIDs;

db.Groups.aggregate([
    {
        $unwind: "$members.regularStudent"
    },
    {
        $group: {
            _id: null,
            UPIDs: { $addToSet: "$members.regularStudent" }
        }
    },
    {
        $project: {
            members: {
                $setDifference: [ userProductUPIDs , "$UPIDs" ]
            },
            _id : 0
        }
    }
])

Мой вывод

 {
    "members" : [
        "300",
        "100"
    ]
}

Ответы на вопрос(1)

Ваш ответ на вопрос