How to $setDifference in array & Object using Mongo DB

别来无恙 提交于 2019-12-23 23:12:32

问题


UserDetails

{
    "_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"
}

Groups

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

Step 1

I have to take UID from UserDetails check with UserProducts then take UPID from UserProducts

Step 2

we have to check this UPID mapped to Groups collection or not ?. members.regularStudent we are mapped UPID

Step 3

Suppose UPID not mapped means i want to print the UPID from from UserProducts

I have tried but couldn't complete this, kindly help me out on this.

Expected Output:

["300"]

Note: Expected Output is ["300"] , because UserProducts having UPID 100 & 200 but Groups collection mapped only 100& 200.

My Code

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
        }
    }
])

My Output

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

回答1:


You need to fix that second aggregation and get all UPIDs as an array. To achieve that you can use $cond and based on $type either return an array or use $objectToArray to run the conversion, try:

db.Groups.aggregate([
    {
        $project: {
            students: {
                $cond: [ 
                    { $eq: [ { $type: "$members.regularStudent" }, "array" ] },
                    "$members.regularStudent",
                    { $map: { input: { "$objectToArray": "$members.regularStudent" }, as: "x", in: "$$x.v" } }
                ]
            }
        }
    },
    {
        $unwind: "$students"
    },
    {
        $group: {
            _id: null,
            UPIDs: { $addToSet: "$students" }
        }
    },
    {
        $project: {
            members: {
                $setDifference: [ userProductUPIDs , "$UPIDs" ]
            },
            _id : 0
        }
    }
])


来源:https://stackoverflow.com/questions/54178838/how-to-setdifference-in-array-object-using-mongo-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!