如何从集合中获取最大值

我收藏了很多东西,比如:

db.kids.find()
//results
[
{name:'tom', age:10},
{name:'alice', age:12},
....
]

我需要一个查询得到最大年龄从这个集合 比如 SQL: SELECT MAX(age) FROM kids WHERE 1

224707 次浏览

what about using aggregate framework:

db.collection.aggregate({ $group : { _id: null, max: { $max : "$age" }}});

建议答案的表现良好。根据 MongoDB 文档:

当 $sort 紧跟在 $limit 之前时,优化器可以将 $limit 合并到 $sort 中。这允许 < strong > 排序操作 在进行时仅保持顶部 n 个结果 ,其中 < strong > n 为 指定的限制 ,MongoDB 只需要在 记忆

在4.0版本中更改。

So in the case of

db.collection.find().sort({age:-1}).limit(1)

由于上述优化,我们只得到排序集合的最高元素 没有

简单的解释, if you have mongo query Response something like below - 你只想要 < strong > 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"
}
],


},

那就加上

Latest _ wish _ CreatedDate: { $max: “ $Date”} ,

像下面这样

{
$project : { _id: 1,
TotalWishs : 1 ,
wish:1 ,
Events:1,
Wish_CreatedDate:1,
Latest_Wish_CreatedDate: { $max: "$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"
],
"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"
},

您可以通过运行一个计划来查看优化器正在做什么。查看计划的通用格式来自 MongoDBdocumentation。即 Cursor.plan()。如果你真的想深入挖掘,你可以做一个 cursor.plan(true)的更多细节。

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.

换句话说,“瑜伽士”的建议是正确的。

Thanks - Sumit

对于 max 值,我们可以将 sql 查询写为

select age from table_name order by age desc limit 1

我们也可以用蒙哥布语写。

db.getCollection('collection_name').find().sort({"age" : -1}).limit(1); //max age
db.getCollection('collection_name').find().sort({"age" : 1}).limit(1); //min age
db.collection.findOne().sort({age:-1}) //get Max without need for limit(1)

还可以通过聚合管道实现这一点。

db.collection.aggregate([{$sort:{age:-1}}, {$limit:1}])

下面的查询将得到您的最大年龄没有其他领域,

db.collection.find({}, {"_id":0, "age":1}).sort({age:-1}).limit(1)

下面的查询将得到您的最大年龄以及所有收集字段,

db.collection.find({}).sort({age:-1}).limit(1)