鉴于此样本数据:
[
{
"_id": "5798ae78670bc85f3e3fe0df",
"defines": [
{
"domain": "ism:SecurityLabel",
"name": "access",
"@id": "ntk:access"
},
{
"domain": "ntk:AccessProfileType",
"name": "profileDes",
"@id": "ntk:profileDes"
},
{
"domain": "ntk:AccessType",
"name": "requiresAllOf",
"@id": "ntk:requiresAllOf"
},
{
"domain": "ism:SecurityLabel",
"name": "bannerMark",
"@id": "ism:bannerMark"
},
{
"label": "Need-to-Know Access Profile Type",
"name": "AccessProfileType",
"@id": "ntk:AccessProfileType"
},
{
"label": "Need-to-Know Access Type"
"name": "AccessType",
"@id": "ntk:AccessType"
}
]
},
{
"_id": "5798ae78670bc85f3e3fe0e1",
"defines": [
{
"domain": "ntk:AccessProfileType",
"name": "accessPolicy",
"@id": "ntk:accessPolicy"
},
{
"domain": "ism:SecurityLabel",
"name": "atomicEnergyMarking",
"@id": "ism:atomicEnergyMarking"
},
{
"domain": "ism:SecurityLabel",
"name": "classification",
"@id": "ism:classification"
}
]
}
]
我需要查询defines.domain $in ["ism:SecurityLabel", "ntk:AccessProfileType"]
所在的集合,并返回一个对象,每个域都有一个匹配定义数组,如下所示:
{
"ism:SecurityLabel": [
{
"domain": "ism:SecurityLabel",
"name": "access"
"@id": "ntk:access"
},
{
"domain": "ism:SecurityLabel",
"name": "bannerMark"
"@id": "ism:bannerMark"
},
{
"domain": "ism:SecurityLabel",
"name": "atomicEnergyMarking",
"@id": "ism:atomicEnergyMarking"
},
{
"domain": "ism:SecurityLabel",
"name": "classification",
"@id": "ism:classification"
}
],
"ntk:AccessProfileType": [
{
"domain": "ntk:AccessProfileType",
"name": "profileDes"
"@id": "ntk:profileDes"
},
{
"domain": "ntk:AccessProfileType",
"name": "accessPolicy"
"@id": "ntk:accessPolicy"
}
]
}
我试图匹配然后投影我的结果,但我真的不知道如何将数据从它的格式转换为我需要的格式。
{
$match: {
'defines.domain': { $in: types }
}
},
{
$project: {
defines: {
$filter: {
// ??? not sure what to do ???
}
}
}
}
答案 0 :(得分:0)
请检查此查询。这不是你所要求的,但我认为更接近。此外,当您发布问题时。请确保您的测试数据良好。你错过了一些逗号。
db.testing.aggregate([
{ $match: {'defines.domain': { $in: ["ism:SecurityLabel", "ntk:AccessProfileType"] } } },
{$unwind:"$defines"},
{$match:{"defines.domain":{$in:["ism:SecurityLabel", "ntk:AccessProfileType"]}}},
{$group:{"_id":"$defines.domain",data:{$push:"$defines"}}}
]).pretty()