[英]Merge mongodb arrays in one array with aggregation
I want to display the following result我想显示以下结果
[
{
"MergedTabs": [1,2,3,4,5,6],
"name": "John"
}
]
from the collection below从下面的集合
[
{
"name":"John",
"tab":[1,2,3]
},
{
"name":"John",
"tab":[4,5,6]
}
]
I tried the query below but I doesn't display what I want我尝试了下面的查询,但没有显示我想要的
db.collection.aggregate([
{
"$group": {
"_id": "$name",
"MergedTabs": {
"$push": "$tab"
}
}
},
{
"$project": {
"name": "$_id",
"MergedTabs": 1
}
}
])
Any ideas how to solve it please ?任何想法如何解决它? Thanks
谢谢
db.collection.aggregate([
{
$unwind: "$tab"
},
{
$group: {
_id: "$name",
mergedTab: {
$push: "$tab"
}
}
},
{
$project: {
name: "$_id",
mergedTab: 1,
_id: "$$REMOVE"
}
}
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.