Mongodb 如何使用mongo查询检查两个数组中的$setDifference
用户详细信息Mongodb 如何使用mongo查询检查两个数组中的$setDifference,mongodb,mongodb-query,aggregation-framework,aggregate,Mongodb,Mongodb Query,Aggregation Framework,Aggregate,用户详细信息 { "_id" : "5c23536f807caa1bec00e79b", "UID" : "1", "name" : "A", }, { "_id" : "5c23536f807caa1bec00e78b", "UID" : "2", "name" : "B", } 用户产品 { "_id" : "5c23536f807caa1bec00e79c", "UPID" : "100", "UID" : "1"
{
"_id" : "5c23536f807caa1bec00e79b",
"UID" : "1",
"name" : "A",
},
{
"_id" : "5c23536f807caa1bec00e78b",
"UID" : "2",
"name" : "B",
}
用户产品
{
"_id" : "5c23536f807caa1bec00e79c",
"UPID" : "100",
"UID" : "1"
},
{
"_id" : "5c23536f807caa1bec00e79c",
"UPID" : "200",
"UID" : "2"
}
团体
{
"_id" : "5bb20d7556db6915846da55f",
"members" : {
"regularStudent" : [
"200" // UPID
],
}
}
第一步
我必须从用户详细信息中获取UID检查用户产品,然后从用户产品中获取UPID
步骤2
我们必须检查该UPID
是否映射到组集合?。
members.regularlstudent
我们被映射到UPID
步骤3
假设UPID未映射意味着我想从UserProducts打印UPID
我已经试过了,但无法完成,请帮我解决这个问题
预期产出:
注:预期输出为[“100”]
,因为用户产品具有UPID100和200
,但组只映射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这样的返回错误
db.Groups.aggregate([
{
$unwind: "$members.regularStudent"
},
{
$group: {
_id: null,
UPIDs: { $addToSet: "$members.regularStudent" }
}
},
{
$project: {
members: {
$setDifference: [ $userProductUPIDs , "$members" ]
},
_id : 0
}
}
])
由于这是我之前的一个答案的后续,我将尝试修复您的代码。底线是您需要两个查询,因为您无法升级数据库,因此代码应如下所示:
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
回复null@KanagarajSa现在应该没事了不,我用了你的代码,它返回
{“members”:null}
@KanagarajSa请确保在查询之间正确传递数据,这里你可以验证最后一部分:我复制并粘贴了你的完整代码并在我的机器上运行,但不工作它对你有效吗?
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