聚合创建对象 [英] Create object in aggregation

查看:60
本文介绍了聚合创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Aggregation Framework按UserId分组后,我得到此结果.我想创建一个具有合并"数组foo和功能的单个文档

I have this result after group by UserId with Aggregation Framework. I want create a single Document with "merge" array foo and ability

我的聚集

{
    "result" : [ 
        {
            "_id" : {
                "userId" : ObjectId("53bab268ceee750615240269")
            },
            "foo" : [ 
                "0.109", 
                "0.105", 
                "0.50", 
                "0.1", 
                "foo"
            ],
            "ability" : [ 
                "Power", 
                "Energy", 
                "ReactiveEnergy", 
                "Stamina", 
                "bar"
            ]
        }
    ],
    "ok" : 1
}

我喜欢拥有

{
    "result" : [ 
        {
            "_id" : {
                "userId" : ObjectId("53bab268ceee750615240269")
            },
            "fooFinal" : [ 
                {"0.109",power} 
                {"0.105",Energy }
                {"0.50",ReactiveEnergy }
                {"0.1",Stamina }
                {"foo",bar}
            ],
        }
    ],
    "ok" : 1
}

我想使用类似$ each的方法,但是我不能在聚合中使用它

I want to use like similar $each, but i can't use it inside aggregate

推荐答案

尝试在您的$ group中做到这一点

Try to do This in your $group

 $group:{
    _id:{'userId':'$userId'},
    'fooFinal' : {$push:{
        foo:'$foo',
        ability:'$ability'}
    }
}

这篇关于聚合创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆