Skip to content Skip to sidebar Skip to footer

How To Print Minimum Result In Mongodb

MongoDB noob here... So, I'm trying to print out the minimum value score inside a collection that looks like this... > db.students.find({'_id': 1}).pretty() {

Solution 1:

You've got the right idea, but in the last step of the aggregation what you want to do is group all the scores by student and find the $min value.

Change the last pipeline operation to:

{ $group: {
        _id: "$_id",
        minScore: {$min: "$scores.score"}
    }}

Solution 2:

> db.students.aggregate(     
{ $unwind:  "$scores" },`
{ $match:{"scores.type":"homework"} }, 
{ $group: { 
    _id : "$_id", 
   maxScore : { $max : "$scores.score"}, 
   minScore: { $min:"$scores.score"} 
  }
});

how to aggregate on each item in collection in mongoDB

Post a Comment for "How To Print Minimum Result In Mongodb"