0

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

1 Answer 1

1

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" }
        }
    }    
])
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.