I have a mongodb collection like:
db.kids.find()
//results
[
{name:'tom', age:10},
{name:'alice', age:12},
....
]
I need a query to get MAX 'age' from this collection
like in SQL: SELECT MAX(age) FROM kids WHERE 1
I have a mongodb collection like:
db.kids.find()
//results
[
{name:'tom', age:10},
{name:'alice', age:12},
....
]
I need a query to get MAX 'age' from this collection
like in SQL: SELECT MAX(age) FROM kids WHERE 1
As one of comments:
db.collection.find().sort({age:-1}).limit(1) // for MAX
db.collection.find().sort({age:+1}).limit(1) // for MIN
it's completely usable but i'm not sure about performance
The performance of the suggested answer is fine. According to the MongoDB documentation:
When a $sort immediately precedes a $limit, the optimizer can coalesce the $limit into the $sort. This allows the sort operation to only maintain the top n results as it progresses, where n is the specified limit, and MongoDB only needs to store n items in memory.
So in the case of
db.collection.find().sort({age:-1}).limit(1)
we get only the highest element WITHOUT sorting the collection because of the mentioned optimization.
what about using aggregate framework:
db.collection.aggregate({ $group : { _id: null, max: { $max : "$age" }}});
Simple Explanation, if you have mongo query Response something like below - and you want only highest value from Array-> "Date"
{
"_id": "57ee5a708e117c754915a2a2",
"TotalWishs": 3,
"Events": [
"57f805c866bf62f12edb8024"
],
"wish": [
"Cosmic Eldorado Mountain Bikes, 26-inch (Grey/White)",
"Asics Men's Gel-Nimbus 18 Black, Snow and Fiery Red Running Shoes - 10 UK/India (45 EU) (11 US)",
"Suunto Digital Black Dial Unisex Watch - SS018734000"
],
"Date": [
"2017-02-13T00:00:00.000Z",
"2017-03-05T00:00:00.000Z"
],
"UserDetails": [
{
"createdAt": "2016-09-30T12:28:32.773Z",
"jeenesFriends": [
"57edf8a96ad8f6ff453a384a",
"57ee516c8e117c754915a26b",
"58a1644b6c91d2af783770b0",
"57ef4631b97d81824cf54795"
],
"userImage": "user_profile/Male.png",
"email": "roopak@small-screen.com",
"fullName": "Roopak Kapoor"
}
],
},
***Then you have add
Latest_Wish_CreatedDate: { $max: "$Date"},
somthing like below-
{
$project : { _id: 1,
TotalWishs : 1 ,
wish:1 ,
Events:1,
Wish_CreatedDate:1,
Latest_Wish_CreatedDate: { $max: "$Date"},
}
}
And Final Query Response will be below
{
"_id": "57ee5a708e117c754915a2a2",
"TotalWishs": 3,
"Events": [
"57f805c866bf62f12edb8024"
],
"wish": [
"Cosmic Eldorado Mountain Bikes, 26-inch (Grey/White)",
"Asics Men's Gel-Nimbus 18 Black, Snow and Fiery Red Running Shoes - 10 UK/India (45 EU) (11 US)",
"Suunto Digital Black Dial Unisex Watch - SS018734000"
],
"Wish_CreatedDate": [
"2017-03-05T00:00:00.000Z",
"2017-02-13T00:00:00.000Z"
],
"UserDetails": [
{
"createdAt": "2016-09-30T12:28:32.773Z",
"jeenesFriends": [
"57edf8a96ad8f6ff453a384a",
"57ee516c8e117c754915a26b",
"58a1644b6c91d2af783770b0",
"57ef4631b97d81824cf54795"
],
"userImage": "user_profile/Male.png",
"email": "roopak@small-screen.com",
"fullName": "Roopak Kapoor"
}
],
"Latest_Wish_CreatedDate": "2017-03-05T00:00:00.000Z"
},
Folks you can see what the optimizer is doing by running a plan. The generic format of looking into a plan is from the MongoDB documentation . i.e. Cursor.plan(). If you really want to dig deeper you can do a cursor.plan(true) for more details.
Having said that if you have an index, your db.col.find().sort({"field":-1}).limit(1) will read one index entry - even if the index is default ascending and you wanted the max entry and one value from the collection.
In other words the suggestions from @yogesh is correct.
Thanks - Sumit
you can use group and max:
db.getCollection('kids').aggregate([
{
$group: {
_id: null,
maxQuantity: {$max: "$age"}
}
}
])