Imagine I have a file in my mongo database with various documents stored in the CARS collection.
If I want to sort them, I can use:
db.cars.find().sort({car:1, price:-1})
Now, I am looking for a query that will return the top car of each category based on 'car'. Specifically, I would like to add a new field called "price_high" to these selections using
{$set:{"price_high"},{multi:true}}
.
'car':Chevy, 'price':100 //This is the top one from Chevy
'car':Chevy, 'price': 80
'car':Chevy, 'price': 60
'car':Lexus, 'price':99 //Top Lexus
'car':Lexus, 'price':90
'car':Lexus, 'price':85
'car':Maserati, 'price':99 //Top Maserati
'car':Maserati, 'price':96
'car':Maserati, 'price':93
This query should be executed only in the MONGO TERMINAL, not written into a file!