如何使用mongo-query在两个数组中检查$ setDifference [英] How to check $setDifference in two array using mongo-query

查看:110
本文介绍了如何使用mongo-query在两个数组中检查$ setDifference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UserDetails

UserDetails

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

UserProducts

UserProducts

{
    "_id" : "5c23536f807caa1bec00e79c",
    "UPID" : "100",
    "UID" : "1"
},
{
    "_id" : "5c23536f807caa1bec00e79c",
    "UPID" : "200",
    "UID" : "2"
}

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

第1步

我必须从 UserDetails 中获取UID,并与 UserProducts 进行核对,然后从 UserProducts

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

第2步

我们必须检查是否映射到 Groups 集合的UPID吗? members.regularStudent我们被映射为UPID

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

第3步

假设未映射 UPID ,这意味着我要从 UserProducts

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:

["100"]

注意:预期输出为["100"],因为 UserProducts 具有UPID 100 & 200 Groups 集合仅映射了200.

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

我的代码

db.UserDetails.aggregate(
{
$lookup: {
    from: "UserProducts",
    localField: "UID",
    foreignField: "UID",
    as: "userProduct"
    }
},
{ $unwind: "$userProduct" },
{
    "$project": { "_id" : 0, "userProduct.UPID" : 1 }
},
{
    $group: {
        _id: null,
        userProductUPIDs: { $addToSet: "$userProduct.UPID" }
    }
}
) // returns [ "100", "200" ]

db.Groups.aggregate([
    {
        $unwind: "$members.regularStudent"
    },
    {
        $group: {
            _id: null,
            UPIDs: { $addToSet: "$members.regularStudent" }
        }
    }
]) // returns ["200"]

现在我想检查两个数组的 $ setDifference ,所以我添加了以下代码,但返回了$userProductUPIDs is not defined

Now i want to check $setDifference of both array, so i had added below code but returning error like $userProductUPIDs is not defined

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

推荐答案

由于这是我以前的回答之一的后续措施,因此我将尝试修复您的代码.底线是您需要两个查询,因为您无法升级数据库,因此代码应如下所示:

As this is a follow up to one of my previous answers I will try to fix your code. The bottom line is that you need two queries as you can't upgrade your database so the code should look like below:

var queryResult = db.UserDetails.aggregate(
{
$lookup: {
    from: "UserProducts",
    localField: "UID",
    foreignField: "UID",
    as: "userProduct"
    }
},
{ $unwind: "$userProduct" },
{
    "$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
        }
    }
]) // should return 100

这篇关于如何使用mongo-query在两个数组中检查$ setDifference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆