Arrays 从mongoDB中的不同对象连接所有数组

Arrays 从mongoDB中的不同对象连接所有数组,arrays,mongodb,mongodb-query,concatenation,aggregation-framework,Arrays,Mongodb,Mongodb Query,Concatenation,Aggregation Framework,我正在开始发现mongodb,我希望将图像放在同一个数组中 例如: {user_id:01, images:[10,20,30,40,50]} {user_id:02, images:[11,22,33,44,55]} {user_id:03, images:[12,22,32,42,52]} {user_id:04, images:[13,23,33,43,53]} 想要的结果是: images:[10,20,30,40,50,11,22,33,44,55,12,22,32,42,52,1

我正在开始发现
mongodb
,我希望将
图像
放在同一个数组中

例如:

{user_id:01, images:[10,20,30,40,50]}
{user_id:02, images:[11,22,33,44,55]}
{user_id:03, images:[12,22,32,42,52]}
{user_id:04, images:[13,23,33,43,53]}
想要的结果是:

 images:[10,20,30,40,50,11,22,33,44,55,12,22,32,42,52,13,23,33,43,53]
我通过文档找到了
$aggregation
解决方案,以下是我尝试过的:

  db.results.aggregate([{ $project: { images: { $concatArrays: ["$images"]}}}])
但是它只是从同一个对象中提取数组,在我的例子中,正如我在
result want
中提到的那样,要从不同的对象中提取所有名为
images
的数组,需要两个步骤:

使用常量值(如
null
)作为分组
\u id
,以获取数组数组(来自所有文档)并展平所有数组

db.collection.aggregate([
    {
        $group: {
            _id: null,
            images: { $push: "$images"}
        }
    },
    {
        $project: {
            _id: 0,
            images: {
                $reduce: {
                    input: "$images",
                    initialValue: [],
                    in: {
                        $concatArrays: [ "$$this", "$$value"]
                    }
                }
            }
        }
    }
])

您可以运行以下聚合查询以获得结果:

db.collection.aggregate([
{
    $unwind: "$images" // flatten the "images" array into separate documents
},
{
    $group: { // group all documents
        "_id": null, // into the same bucket
        "images": { $push: "$images"} // and push every image entry into an array called "images"
    }
},
{
    $project: {
        _id: 0 // to get rid of the "_id" field if needed
    }
}])

感谢您的帮助,我尝试执行此查询,但它给了我以下错误:
2018-05-08T14:56:35.743+0000 E query[thread1]语法错误:缺少:在属性id@(shell):5:16之后通过mongo CLI,顺便说一句,我的
mongoDB版本是3.4
它正在工作,我用空格命名字段,这就是它不工作的原因谢谢:-),有没有办法不显示标签字段
图像
,因为我被假定为将所有这些数量的
图像
分组在一个列表中file@Data_Geek聚合的输出必须是有效的JSON(BSON)因此,你可以在文档中使用数组,也可以在多个文档中使用单个项,这不是你想要实现的,我想我明白你的意思了,无论如何,感谢你的时间和帮助这是解决方案的第2版,但是如何不显示
\u id
字段?有人能解释一下为什么这个答案被否决了吗,拜托?我不知道怎么回事,我注意到这个问题,答案不知怎么被否决了