1

Let me use following pseudo example to better explain my need.

I have a group of people and some images which belong to group schema

{
  images:['ARRAY OF IMAGES'],
  members:[{
    age:NUMBER,
    name:STRING, 
    email:EMAIL_OF_MEMBER
  }], 
  groupName:String
  ....etc
}

Now i have a aggregation query

group.aggregate([
  {
    $project:{
       //some code happening to support query in the below $match
       'original':$$ROOT
    }
  },
  {
    $match:{//some query code happening and all params from $project above will be available here.  the $original field contains all fields of document}
  },
  {
    //Now i want to extract 1st image from Images array, Min and Max age of members in group.
  }
],function(err,results){//dosomething with results})

If I don't use 3rd pipeline I am getting the whole document which is not required for me. I just need 1 image and min-max age of people in group to display a web page. I don't need other details.

1 Answer 1

2

Below part of the query will gives you the first image as well as min and max age of the members.

db.[collection].aggregate([
                            { 
                              $unwind : "$members" 
                            },
                            { 
                              $group : 
                                     { _id: "$_id" , 
                                       images : { $first: "$images"},
                                       minAge : {$min : "$members.age"},
                                       maxAge : {$max: "$members.age"}
                                     }
                            }
                          ]).pretty();

For the mongoDb Version 3.1.6 and above, you can use $Slice in aggregation pipeline to limit the contents of array.

db.[collection].aggregate([
                            { 
                              $unwind : "$members" 
                            },
                            { 
                              $group : 
                                     { _id: "$_id" , 
                                       images : {$push : "$images"},
                                       minAge : {$min : "$members.age"},
                                       maxAge : {$max: "$members.age"}
                                     }
                            },
                            { 
                              $project : 
                                       { images : 
                                           { images :{$slice:1} }
                                       },
                                       minAge : 1 ,
                                       maxAge  : 1
                           }
                         ]).pretty();
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.