【发布时间】:2019-10-19 11:12:56
【问题描述】:
我试图在 $unwind 操作之后对一组文档进行分组。我的文档如下所示:
{
"_id" : ObjectId("5cdb5b5acadf5100019da2f4"),
"allowedLocations" : [
{
"type" : "country",
"value" : "world",
"label" : "World"
}
],
"disallowedLocations" : [
{
"type" : "country",
"value" : "CF",
"label" : "Central African Republic"
},
{
"type" : "country",
"value" : "CN",
"label" : "China"
}
],
}
{
"_id" : ObjectId("5cdb5b5acadf5100019da2f4"),
"allowedLocations" : [
{
"type" : "country",
"value" : "US",
"label" : "United States of America"
}
],
"disallowedLocations" : [
{
"type" : "country",
"value" : "CA",
"label" : "Canada"
},
{
"type" : "country",
"value" : "MX",
"label" : "Mexico"
}
],
}
我想按 _id 对它们进行分组,然后将 allowedLocations 和 disallowedLocations 数组连接为一个。我的管道中的小组赛阶段如下所示:
{
"$group" : {
"_id" : "$_id",
"allowedLocations" : {
"$push" : "$allowedLocations"
},
"disallowedLocations" : {
"$push" : "disallowedLocations"
}
}
}
问题是,我得到的结果不是两个数组串联的文档,而是数组的数组,数组的每个元素都是每个文档的数组:
{
"_id" : ObjectId("5cdb5b5acadf5100019da2f4"),
"allowedLocations" : [
[
{
"type" : "country",
"value" : "US",
"label" : "United States of America"
}
],
[
{
"type" : "country",
"value" : "world",
"label" : "World"
}
],
],
"disallowedLocations" : [
[
{
"type" : "country",
"value" : "CF",
"label" : "Central African Republic"
},
{
"type" : "country",
"value" : "CN",
"label" : "China"
}
],
[
{
"type" : "country",
"value" : "CA",
"label" : "Canada"
},
{
"type" : "country",
"value" : "MX",
"label" : "Mexico"
}
]
}
}
有没有办法生成一个只有对象作为元素的平面数组?在推送之前我也尝试过使用 $concatArrays ,但这会在数组中创建更多数组。
【问题讨论】:
-
在执行
$group之前为什么不直接$unwinddisallowedLocations和allowedLocations?
标签: mongodb mongodb-query aggregation-framework