Bazinga777
Bazinga777

Reputation: 5291

Project array of objects from results into array of arrays

I have a mongodb query that results data in the following format

[{"_id":1471424941,"value":[1444,0]},{"_id":1471424941,"value":[1444,0]}]

and I would like to convert the result into

[[1471424941, 1444,0],[1471424941, 1444,0]]

Will this be possible using aggregate method ? I want to avoid using Javascript for the conversion and want to do it using mongodb if possible.

Can MongoDb produce an aggreation that will remote the value of the keys from the result

Upvotes: 0

Views: 758

Answers (1)

chridam
chridam

Reputation: 103455

Run the following aggregation pipeline which uses the $concatArrays operator to concatenate arrays and get the desired result as a key:

db.collection.aggregate([
    {
        "$project": {
            "items": { "$concatArrays": [ "$value", ["$_id"]  ] }
        }
    },
    {
        "$group": {
            "_id": null,
            "items": { "$push": "$items" }
        }
    }    
])

Upvotes: 1

Related Questions