// orders collection
[
{
"id": 1,
"orderName": "a",
"seqId": 100,
"etc": [],
"desc": [],
},
{
"id": 2,
"orderName": "b",
"seqId": 200,
"etc": [],
"desc": []
},
{
"id": 3,
"orderName": "c",
"seqId": 100,
"etc": [],
"desc": [],
},
]
// goods collection
[
{
"id": 1,
"title": "example1",
"items": [
{
"id": 10,
"details": [
{
"id": 100
},
{
"id": 101,
}
]
},
{
"id": 20,
"details": [
{
"id": 102,
},
{
"id": 103,
}
]
},
]
},
[
{
"id": 2,
"title": "example2",
"items": [
{
"id": 30,
"details": [
{
"id": 200
},
{
"id": 201
}
]
},
{
"id": 40,
"details": [
{
"id": 202
},
{
"id": 203
}
]
},
]
},
]
当订单集合的etc字段和desc字段数组为空的文档的seqId字段的值与货物集合的"www.example.com"字段的值相同时,我希望得到以下输出。goods.details.id field of the goods collection are the same, I want to get the following output. How can I do that?
[
{orderName: "a", title: "example1"},
{orderName: "b", title: "example2"},
{orderName: "c", title: "example1"},
]
另外,我想根据货物集合的标题执行求和操作。
[
{"example1": 2},
{"example2": 1}
]
1条答案
按热度按时间y3bcpkx11#
只需在
orders.seqId
和goods.items.details.id
之间执行$lookup
。使用$unwind
消除空查找(即内部连接行为)。最后,使用$sum
执行$group
以获得计数。Mongo Playground