Stephan
Stephan

Reputation: 461

MongoDB aggregate: retrieve data in single array

I have millions of documents with the following schema:

{
   _id: '3fbwehgzgfwehgrqwegrqwer',
   someData: [0,1],
   moreData: {
       key: true
   } 
},

{
   _id: '24nj5h219ebwjfqwverqwer',
   someData: [2,3],
   moreData: {
       key: true
   } 
},

I need the someData array combined in a result array like:

{
    result: [
       [0,1],
       [2,3] 
    ]
}

Upvotes: 0

Views: 1948

Answers (1)

sergiuz
sergiuz

Reputation: 5529

Using aggregation pipeline, you can $group by _id: null, than $push all $someData in result field:

db.collection.aggregate([
    {"$group":{_id: null, result: {$push: "$someData"}}} 
]).pretty()

Result:

{ "_id" : null, "result" : [ [ 0, 1 ], [ 2, 3 ] ] }

Upvotes: 5

Related Questions