1

I have a data set that looks like the one below

[{
 _id: ObjectID(),
 data: {
    key1: value,
    key2: value
 }
},
{ 
_id: ObjectId(),
data: {
  key1: value,
  key2: value
}
}]

I want to convert the same into

[
 [_id,key1,key2], [_id,key1,key2]
]

I have tried the aggregation framework but couldn't come up with any way of doing this. I want to avoid looping over the objects if possible but don't know if it would be possible to do so.

1 Answer 1

4

Nothing to explain really. Pretty basic $project and $group:

db.collection.aggregate([
    {$project : {data : ["$_id", "$data.key1", "$data.key2"]}},
    {$group : {_id : null, result : {$push : "$data"}}}
]);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.