我有一个mongodb集合,例如:
db.kids.find()
//results
[
{name:'tom', age:10},
{name:'alice', age:12},
....
]
我需要一个查询来从此集合中获取MAX“年龄”,如SQL: SELECT MAX(age) FROM kids WHERE 1
我有一个mongodb集合,例如:
db.kids.find()
//results
[
{name:'tom', age:10},
{name:'alice', age:12},
....
]
我需要一个查询来从此集合中获取MAX“年龄”,如SQL: SELECT MAX(age) FROM kids WHERE 1
db.collection.find().sort({age:-1}).limit(1)
Answers:
db.collection.find().sort({age:-1}).limit(1) // for MAX
db.collection.find().sort({age:+1}).limit(1) // for MIN
它是完全可用的,但我不确定性能
age
字段上定义一个索引。然后,如果使用db.collection.find({}, {age: 1, _id:0}).sort({age:-1}).limit(1)
,则可能会有一个非常快速的Covered Query
建议答案的效果很好。根据MongoDB文档:
当$ sort紧接在$ limit之前时,优化程序可以将$ limit合并到$ sort中。这允许排序操作在执行过程中仅保留前n个结果,其中n是指定的限制,而MongoDB仅需要在内存中存储n个项目。
在版本4.0中更改。
所以在
db.collection.find().sort({age:-1}).limit(1)
由于提到的优化,我们只获得最高的元素而无需对集合进行排序。
find( ... ).sort( ... ).limit( ... )
与对待方式相同aggregate([{$match: ... }, {$sort: ...}, {$limit: ...}])
吗?他们在mongo文档中有没有提到这一点的地方?
如何使用聚合框架:
db.collection.aggregate({ $group : { _id: null, max: { $max : "$age" }}});
您可以使用group和max:
db.getCollection('kids').aggregate([
{
$group: {
_id: null,
maxQuantity: {$max: "$age"}
}
}
])
db.collection.findOne().sort({age:-1}) //get Max without need for limit(1)
TypeError: db.collection.findOne(...).sort is not a function
。collection.findOne()返回文档本身,因此调用sort()似乎不太可行。
简单说明,如果您有mongo查询,则响应如下所示-并且您只希望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"
},