1

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

My Aggregation

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

I like to have

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

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

3
  • That would not be a valid structure for the result. Do you actually mean just getting those values under different keys or as arrays of the "pairs"? Commented Jul 10, 2014 at 10:30
  • 1
    Additionally, it would really help to have some idea of the structure of the original documents you are getting this result from. All you have posted is what your aggregation result currently is and what you want to get ( with an invalid structure ). It helps to know where you are coming "from" as a source document and also what you are currently doing, as in your current aggregation operation. Commented Jul 10, 2014 at 10:55
  • As at MongoDB 2.6, there is no array indexing operator in the Aggregation Framework so I'm not sure how you would combine the two arrays. It would definitely be helpful to see what document format you started with as there may be an easier way to make the transformation. I would also suggest upvoting/watching SERVER-4589 in the MongoDB Jira issue tracker, which is the feature request for an array indexing operator. Commented Jul 21, 2014 at 5:00

1 Answer 1

4

Try to do This in your $group

 $group:{
    _id:{'userId':'$userId'},
    'fooFinal' : {$push:{
        foo:'$foo',
        ability:'$ability'}
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.