我找不到任何地方有这样的记录。默认情况下,find()操作将从头获取记录。我怎么能得到mongodb的最后N条记录?
编辑:我也想返回的结果从最近到最近的顺序,而不是相反。
我找不到任何地方有这样的记录。默认情况下,find()操作将从头获取记录。我怎么能得到mongodb的最后N条记录?
编辑:我也想返回的结果从最近到最近的顺序,而不是相反。
当前回答
@bin-chen,
可以对集合中文档子集的最新n个条目使用聚合。下面是一个没有分组的简化示例(在本例中,您将在阶段4和阶段5之间进行分组)。
这将返回最新的20个条目(基于一个称为“时间戳”的字段),按升序排序。然后它将每个文档_id、时间戳和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
}
希望这能有所帮助。
其他回答
你可能想要使用find选项: http://docs.meteor.com/api/collections.html#Mongo-Collection-find
db.collection.find({}, {sort: {createdAt: -1}, skip:2, limit: 18}).fetch();
根据集合的大小,排序、跳过等操作可能会非常慢。
如果您根据某些标准对集合进行索引,则可以获得更好的性能;然后你可以使用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/
如果我理解了你的问题,你需要按升序排序。
假设你有一些id或日期字段称为“x”,你会做…
.sort ()
db.foo.find().sort({x:1});
1将升序排序(从最老到最新),而-1将降序排序(从最新到最老)。
如果你使用自动创建的_id字段,它有一个日期嵌入其中…所以你可以用它来订购…
db.foo.find().sort({_id:1});
这将返回从最老到最新排序的所有文档。
自然秩序
你也可以使用上面提到的自然顺序……
db.foo.find().sort({$natural:1});
同样,使用1或-1取决于你想要的顺序。
使用.limit ()
最后,在执行这种广泛开放的查询时添加一个限制是一个很好的实践,这样你就可以这样做…
db.foo.find().sort({_id:1}).limit(50);
or
db.foo.find().sort({$natural:1}).limit(50);
db.collection.find().hint( { $natural : -1 } ).sort(field: 1/-1).limit(n)
根据mongoDB文档:
您可以指定{$natural: 1}强制查询执行转发集合扫描。 您还可以指定{$natural: -1}来强制查询执行反向收集扫描。
@bin-chen,
可以对集合中文档子集的最新n个条目使用聚合。下面是一个没有分组的简化示例(在本例中,您将在阶段4和阶段5之间进行分组)。
这将返回最新的20个条目(基于一个称为“时间戳”的字段),按升序排序。然后它将每个文档_id、时间戳和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
}
希望这能有所帮助。