如果我理解你的问题,你需要按升序排序。
假设您有一些名为“x”的 id 或日期字段,您会这样做......
。种类()
db.foo.find().sort({x:1});
1 将按升序排序(从最旧到最新),-1 将按降序排序(从最新到最旧)。
如果您使用自动创建的 _id 字段,它会嵌入一个日期...所以您可以使用它来订购...
db.foo.find().sort({_id:1});
这将返回从最旧到最新排序的所有文档。
自然秩序
您也可以使用上面提到的 Natural Order ...
db.foo.find().sort({$natural:1});
同样,根据您想要的顺序使用 1 或 -1。
使用 .limit()
最后,在进行这种广泛开放的查询时添加限制是一种很好的做法,这样您就可以这样做......
db.foo.find().sort({_id:1}).limit(50);
或者
db.foo.find().sort({$natural:1}).limit(50);
通过以下查询可以看到最后 N 个添加的记录,从最近到最近:
db.collection.find().skip(db.collection.count() - N)
如果您希望它们以相反的顺序排列:
db.collection.find().sort({ $natural: -1 }).limit(N)
如果您安装 Mongo-Hacker,您还可以使用:
db.collection.find().reverse().limit(N)
如果您厌倦了一直编写这些命令,您可以在 ~/.mongorc.js 中创建自定义函数。例如
function last(N) {
return db.collection.find().skip(db.collection.count() - N);
}
然后从 mongo shell 中输入 last(N)
db.collection.find().reverse().limit(1)
给我错误... has no method reverse
根据集合的大小,排序、跳过等可能会非常慢。
如果您的收藏按某些标准编制索引,将会获得更好的性能;然后你可以使用 min() 游标:
首先,使用 db.collectionName.setIndex( yourIndex )
索引您的集合您可以使用升序或降序,这很酷,因为您总是想要“最后 N 个项目”......所以如果您按降序索引它与获取“第一个N 项”。
然后你找到你的集合的第一个项目,并使用它的索引字段值作为搜索中的最小标准,例如:
db.collectionName.find().min(minCriteria).hint(yourIndex).limit(N)
下面是 min() 游标的参考:https://docs.mongodb.com/manual/reference/method/cursor.min/
db.collectionName.find().hint(yourIndex).limit(N)
如果索引按降序排列,您将获得 N 个最小值。
为了获取最后 N 条记录,您可以执行以下查询:
db.yourcollectionname.find({$query: {}, $orderby: {$natural : -1}}).limit(yournumber)
如果您只想要最后一条记录:
db.yourcollectionname.findOne({$query: {}, $orderby: {$natural : -1}})
注意:您可以使用集合中的某一列来代替 $natural
。
@陈斌,
您可以对集合中文档子集的最新 n 个条目使用聚合。这是一个没有分组的简化示例(在这种情况下,您将在第 4 阶段和第 5 阶段之间进行)。
这将返回最新的 20 个条目(基于名为“timestamp”的字段),按升序排序。然后它将每个文档 _id、timestamp 和whatever_field_you_want_to_show 投影到结果中。
var pipeline = [
{
"$match": { //stage 1: filter out a subset
"first_field": "needs to have this value",
"second_field": "needs to be this"
}
},
{
"$sort": { //stage 2: sort the remainder last-first
"timestamp": -1
}
},
{
"$limit": 20 //stage 3: keep only 20 of the descending order subset
},
{
"$sort": {
"rt": 1 //stage 4: sort back to ascending order
}
},
{
"$project": { //stage 5: add any fields you want to show in your results
"_id": 1,
"timestamp" : 1,
"whatever_field_you_want_to_show": 1
}
}
]
yourcollection.aggregate(pipeline, function resultCallBack(err, result) {
// account for (err)
// do something with (result)
}
所以,结果看起来像:
{
"_id" : ObjectId("5ac5b878a1deg18asdafb060"),
"timestamp" : "2018-04-05T05:47:37.045Z",
"whatever_field_you_want_to_show" : -3.46000003814697
}
{
"_id" : ObjectId("5ac5b878a1de1adsweafb05f"),
"timestamp" : "2018-04-05T05:47:38.187Z",
"whatever_field_you_want_to_show" : -4.13000011444092
}
希望这可以帮助。
你可以试试这个方法:
获取集合中的记录总数
db.dbcollection.count()
然后使用跳过:
db.dbcollection.find().skip(db.dbcollection.count() - 1).pretty()
您不能根据集合的大小“跳过”,因为它不会考虑查询条件。
正确的解决方案是从所需的端点进行排序,限制结果集的大小,然后在必要时调整结果的顺序。
这是一个基于真实世界代码的示例。
var query = collection.find( { conditions } ).sort({$natural : -1}).limit(N);
query.exec(function(err, results) {
if (err) {
}
else if (results.length == 0) {
}
else {
results.reverse(); // put the results into the desired order
results.forEach(function(result) {
// do something with each result
});
}
});
var query = collection.find( { conditions } ).sort({$natural : -1}).reverse().limit(N)
之类的东西。
db.collection.find().sort({$natural: -1 }).limit(5)
您可以使用 sort()
、 limit()
、skip()
从任何跳过的值开始获取最后 N 条记录
db.collections.find().sort(key:value).limit(int value).skip(some int value);
查看查询:排序和自然顺序,http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order 以及游标方法下的 sort() http://www.mongodb.org/display/DOCS/Advanced+Queries
您可能想要使用 find
选项:http://docs.meteor.com/api/collections.html#Mongo-Collection-find
db.collection.find({}, {sort: {createdAt: -1}, skip:2, limit: 18}).fetch();
如果使用 MongoDB 指南针,可以使用排序字段进行过滤,
https://i.stack.imgur.com/YjVCJ.png
使用 $slice
运算符限制数组元素
GeoLocation.find({},{name: 1, geolocation:{$slice: -5}})
.then((result) => {
res.json(result);
})
.catch((err) => {
res.status(500).json({ success: false, msg: `Something went wrong. ${err}` });
});
其中 geolocation
是数据数组,从中我们得到最后 5 条记录。
db.collection.find().hint( { $natural : -1 } ).sort(field: 1/-1).limit(n)
您可以指定 { $natural : 1 } 以强制查询执行前向集合扫描。您还可以指定 { $natural : -1 } 以强制查询执行反向收集扫描。
最后一个函数应该是 sort
,而不是 limit
。
例子:
db.testcollection.find().limit(3).sort({timestamp:-1});
sort
会在 limit
之后?
不定期副业成功案例分享
item = 3; item.add(3).divide(3) == 2; item.divide(3).add(3) == 4;
没有顺序会是什么结果???我同意你的观点,这种颠倒的顺序并不直观。毕竟这不是 SQL,它应该遵循正常的 OO 范例。