【发布时间】:2021-04-05 18:23:42
【问题描述】:
大家好,
我需要一些帮助来使聚合管道正常工作,我一直试图让它工作一段时间,但只完成了一半。 这是我的设置
用户文档如下:
{
"_id": { "$oid": "5fea4f976ca46d93c010d33d" },
.....................
"firstname": "firstname1",
"lastname": "lastname1",
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
..................
"certifications": {
certs: [
{
"_id": "5fea44b96ca46d93c010d330",
"itemID": 1,
"date_certified": "2011-05-30 05:09:48"
},
{
"_id": "5fea44b96ca46d93c010d334",
"itemID": 3,
"date_certified": "2007-01-30 06:01:51"
},
{
"_id": "5fea44b96ca46d93c010d337",
"itemID": 5,
"date_certified": "2007-09-21 16:52:52"
}
]
},
.......................
...................
.....................
},
{
"_id": { "$oid": "5fea4f976ca46d93c010545453" },
.....................
.....................
"firstname": "firstname2",
"lastname": "lastname2",
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
..................
"certifications": {
certs: [
{
"_id": "5fea44b96ca46d93c010d330",
"itemID": 2,
"date_certified": "2011-05-30 05:09:48"
},
{
"_id": "5fea44b96ca46d93c010d334",
"itemID": 4,
"date_certified": "2007-01-30 06:01:51"
}
]
},
.......................
...................
.....................
}
认证文档是关注
[{
"_id": {
"$oid": "5fea44b96ca46d93c010d32e"
},
"itemID": 1,
"acronym": "acro1",
"country": "my Great Country",
"name": {
"key1": "My Key 1 description",
"key2": "My Key 2 description",
"key3": "My Key 3 description",
"key4": "My Key 4 description"
},
"website": "www.myGreatWebsite.com"
},
{
"_id": {
"$oid": "5fea44b96ca46d93c010d32f"
},
"itemID": 2,
"acronym": "acro2",
"country": "my Great Country",
"name": {
"key1": "My Key 1 description",
"key2": "My Key 2 description",
"key3": "My Key 3 description",
"key4": "My Key 4 description"
},
"website": "www.myGreatWebsite.com"
},
{
"_id": {
"$oid": "5fea44b96ca46d93c010d330"
},
"itemID": 3,
"country": "my Great Country",
"name": {
"key1": "My Key 1 description",
"key2": "My Key 2 description",
"key3": "My Key 3 description",
"key4": "My Key 4 description"
},
"website": "www.myGreatWebsite.com"
},
{
"_id": {
"$oid": "5fea44b96ca46d93c010d331"
},
"itemID": 4,
"country": "my Great Country",
"name": {
"key1": "My Key 1 description",
"key2": "My Key 2 description",
"key3": "My Key 3 description",
"key4": "My Key 4 description"
},
"website": "www.myGreatWebsite.com"
},
{
"_id": {
"$oid": "5fea44b96ca46d93c010d332"
},
"itemID": 5,
"country": "my Great Country",
"name": {
"key1": "My Key 1 description",
"key2": "My Key 2 description",
"key3": "My Key 3 description",
"key4": "My Key 4 description"
},
"website": "www.myGreatWebsite.com"
}]
现在我的聚合
db.users.aggregate([
{
'$lookup': {
'from': 'certifications',
'let': {
'certs': '$certifications.certs'
},
'pipeline': [
{
'$match': {
'$expr': {
'$in': [
'$itemID', '$$certs.itemID'
]
}
}
}, {
'$project': {
'_id': 0,
'itemID': '$itemID',
'description': '$name.key1',
'acronyme': '$acronym',
'country': '$country',
'count': {
'$size': '$$certs'
},
'date_cert': '$$certs.date_certified'
}
}
],
'as': 'certifications'
}
}
])
结果是
{
.........
...........
"firstname": "firstname1",
"lastname": "lastname1",
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
..................
certification: {
certs: [
{
itemID: 1,
country: 'my Great Country',
description: 'My Key 1 description',
count: 1,
date_cert: [
0:"2005-01-06 10:48:16"
]
},
]
}
........
.........
},
{
.........
...........
"firstname": "firstname2",
"lastname": "lastname2",
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
..................
certification: {
certs: [
{
itemID: 2,
country: 'my Great Country',
description: 'My Key 2 description',
count: 2,
date_cert: [
"2005-01-06 10:48:16"
"2014-06-21 22:44:56"
]
},
{
itemID: 4,
country: 'my Great Country',
description: 'My Key 4 description',
count: 2,
date_cert: [
"2005-01-06 10:48:16"
"2014-06-21 22:44:56"
]
}
]
}
........
.........
}
但预期的结果是
{
.........
...........
"firstname": "firstname1",
"lastname": "lastname1",
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
..................
certification: {
certs: [
{
itemID: 1,
country: 'my Great Country',
description: 'My Key 1 description',
date_cert: "2005-01-06 10:48:16"
},
],
count: 1,
}
........
.........
},
{
.........
...........
"firstname": "firstname2",
"lastname": "lastname2",
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
..................
certification: {
certs: [
{
itemID: 2,
country: 'my Great Country',
description: 'My Key 2 description',
date_cert: "2005-01-06 10:48:16"
},
{
itemID: 4,
country: 'my Great Country'
description: 'My Key 4 description',
date_cert: "2014-06-21 22:44:56"
}
],
count: 2
}
........
.........
}
基本上,我希望将管道匹配阶段的结果与认证查找的文档合并,以便我可以同时使用两者。 在制定聚合时,任何指针都将不胜感激。
谢谢
【问题讨论】:
标签: mongodb project pipeline lookup aggregation