如果FirstArray
和SecondArray
在"名称"中有{
"_id" : ObjectId("58b8d9e3b2b4e07bff8feed5"),
"FirstArray" : [
{
"Name" : "A",
"Something" : "200 ",
},
{
"Name" : "GF",
"Something" : "100 ",
}
],
"SecondArray" : [
{
"Name" : "BC",
"Something" : "200 ",
},
{
"Name" : "A",
"Something" : "100 ",
}
]
}
共有的元素,我如何在MongoDB中执行返回_id的查询?领域?
这是集合结构:
- set_fact:
dict:
a: 1
b: 2
c: 3
dict2: {}
- set_fact:
dict2: "{{dict2 |combine({item.key: item.value})}}"
when: "{{item.key not in ['a']}}"
with_dict: "{{dict}}"
- debug: var=dict2
答案 0 :(得分:8)
3.6更新:
将$match
与$expr
一起使用。 $expr
允许在$match
阶段使用聚合表达式。
db.collection.aggregate([
{"$match":{
"$expr":{
"$eq":[
{"$size":{"$setIntersection":["$FirstArray.Name","$SecondArray.Name"]}},
0
]
}
}},
{"$project":{"_id":1}}
])
旧版本:
您可以尝试$redact
$setIntersection
来查询。
$setIntersection
将FirstArray
s Name
与SecondArray
s Name
进行比较,并返回通用名称文档数组,后跟$size
和$redact
并将结果与0
进行比较以保留并删除文档。
db.collection.aggregate(
[{
$redact: {
$cond: {
if: {
$eq: [{
$size: {
$setIntersection: ["$FirstArray.Name", "$SecondArray.Name"]
}
}, 0]
},
then: "$$KEEP",
else: "$$PRUNE"
}
}
}, {
$project: {
_id: 1
}
}]
)