-1

Working on one of the homework, and I am having and issue writing the logic. I want to identify the minimum score for type homework and drop it based on the '_id' for that document. Here is what I have so far but I am having trouble extracting the _id. Any help would be great i am very new to this.

db.grades.find({'id': {'type': 'homework', 'score': {$min : 'score'}}})

rmahesh
  • 739
  • 2
  • 14
  • 30

1 Answers1

0

If you want to just find the minimum scores then sort ascending and limit the output to 1:

db.grades.find({
  'type': 'homework'
}).sort({'score': 1}).limit(1)

OR Using aggregate:

db.grades.aggregate(
[ 
{ 
   $match:{
      'type': 'homework'
},
 {
    $group:{
      _id: null,
      minScore: { $min: "$score" }
    }
  },
 {
   $project:{
      "_id":1 
    //This query would return the id where score is minimum
   }
 }
]

)

You can also refer to the following for further help How to find min value in mongodb

fatimasajjad
  • 605
  • 8
  • 16