0

Let's say I have a collection of objects:

{
   _id : 1,
   list1 : [
      { name: 'This is my name', value: 'This is my value' },
      { name: 'name number 2', value: 'value number 2' }
   ],
   list2 : [
      { name: 'Another name', value: 'Another value' }
   ],
   deeper : {
      list3 : [
         { name: 'Another name but even deeper', value: 'Another value but deeper' }
      ]
   }
}

Using aggregation pipeline how would I return a single list of name/value objects? Result should be:

{
   _id : 1,
   combinedList : [
      { name: 'This is my name', value: 'This is my value' },
      { name: 'name number 2', value: 'value number 2' },
      { name: 'Another name', value: 'Another value' },
      { name: 'Another name but even deeper', value: 'Another value but deeper' }
   ]
}

PART 2:

Now, how to flatten deeply nested objects from a whole collection into a single response? Example:

{
   "_id" : 0,
   list1 : [
      { name: 'This is my name', value: 'This is my value' },
      { name: 'name number 2', value: 'value number 2' }
   ],
   list2 : [
      { name: 'Another name', value: 'Another value' }
   ]
},
{
   "_id" : 1,
   list1 : [
      { name: 'This is my name', value: 'This is my value' },
      { name: 'name number 2', value: 'value number 2' }
   ],
   deeper : {
      list2 : [
         { name: 'Another name but even deeper', value: 'Another value but deeper' }
      ]
   }
}

The result should be the same document as above (combinedList).

0

1 Answer 1

1

you can use aggregate and setUnion

db.coll.aggregate([
    {
        $match:{_id:1}  
    },
     { 
         $project: {_id: 0 ,
                    combinedList: { $setUnion: [ "$deeper.list3","$list2", "$list1" ] }} 
     }
   ]
)

result

{
    "result" : [ 
        {
            "combinedList" : [ 
                {
                    "name" : "Another name but even deeper",
                    "value" : "Another value but deeper"
                }, 
                {
                    "name" : "Another name",
                    "value" : "Another value"
                }, 
                {
                    "name" : "name number 2",
                    "value" : "value number 2"
                }, 
                {
                    "name" : "This is my name",
                    "value" : "This is my value"
                }
            ]
        }
    ],
    "ok" : 1
}

http://docs.mongodb.org/manual/reference/operator/aggregation/setUnion/

you have to use version 2.6.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Barno! Can I through one more dimension in there? What if I have a list of objects each with a list. Example: {
I added the example to my original question.

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.