nested $project In Mongo Query not Working

619 views
Skip to first unread message

vijay pawar

unread,
Apr 9, 2014, 4:16:00 AM4/9/14
to mongod...@googlegroups.com
    How Can I Make Nested $Project Make Work Without Using Foreach : Only Using Aggregate
   
    db.college.insert([{
       code: "xyz",
       tags: [ "school", "book", "bag", "headphone", "appliance" ],
       qty: [
              { size: "S", num: 10, color: "blue" , items : {a : 11 , b :23}},
              { size: "M", num: 45, color: "blue" , items : {a : 34 , b :345}},
              { size: "L", num: 100, color: "green" , items : {a : 45 , b :4545}}
            ]
    },
    {
       code: "abc",
       tags: [ "appliance", "school", "book" ],
       qty: [
              { size: "6", num: 100, color: "green", items : {a : 341 , b :656} },
              { size: "6", num: 50, color: "blue" , items : {a : 45 , b :45}},
              { size: "8", num: 100, color: "brown" , items : {a : 451 , b :45}}
            ]
    },
   
    {
       code: "efg",
       tags: [ "school", "book" ],
       qty: [
              { size: "S", num: 10, color: "blue" , items : {a : 34 , b :55}},
              { size: "M", num: 100, color: "blue" , items : {a : 545 , b :65}},
              { size: "L", num: 100, color: "green" , items : {a : 454 , b :45}}
            ]
    },
   
    {
       code: "ijk",
       tags: [ "electronics", "school" ],
       qty: [
              { size: "M", num: 100, color: "green" , items : {a : 1 , b :2}}
            ]
    }])
   
    SCENARIO IS LIKE :
    IF LEVEL == 1 Then Bring "Only" Quantity And "Not" Nested Items In Quantity :
    HERE SINGLE $project Is WORKING :
    Query : db.schools3.aggregate(
                        {
                                  $project : {
                                         id : 1 ,
                                         qty : 1
                                              }
                                 }).result
   
    IF LEVEL == 2 :Then Bring Quantity And Also Items Inside Quantity :
    HERE NESTED $project NOT WORKING :                           
    Query : db.schools3.aggregate(
                                {
                                  $project : {
                                         id : 1 ,                                         
                                         qty : {$project :
                                                            {items  : 1}
                                                 }
                                              }
                                 }).result  
   
    How Can I Make Nested $Project Make Work Without USing Foreach : Only Using Aggregate


Charlie Page

unread,
Apr 9, 2014, 4:49:04 PM4/9/14
to mongod...@googlegroups.com
Hi Vijay,

You will need to $unwind the arrays and then project on the unwound arrays, see the docs here: http://docs.mongodb.org/manual/reference/operator/aggregation/group/#group-title-by-author.  After you do that, to get arrays back in the document structure $group with $push is required, see the docs here: http://docs.mongodb.org/manual/reference/operator/aggregation/group/#group-title-by-author.

Unwind is the aggregation equivalent of foreach for arrays.  You many have to unwind multiple times for deeply nested arrays.  Generally speaking I would recommend a different document structure if you have deeply nested arrays.

- C
Reply all
Reply to author
Forward
0 new messages