我找不到任何地方有这样的记录。默认情况下,find()操作将从头获取记录。我怎么能得到mongodb的最后N条记录?

编辑:我也想返回的结果从最近到最近的顺序,而不是相反。


当前回答

你可能想要使用find选项: http://docs.meteor.com/api/collections.html#Mongo-Collection-find

db.collection.find({}, {sort: {createdAt: -1}, skip:2, limit: 18}).fetch();

其他回答

使用.sort()和.limit()

按升序或降序使用Sort,然后使用limit

db.collection.find({}).sort({ any_field: -1 }).limit(last_n_records);
 db.collection.find().sort({$natural: -1 }).limit(5)

最后一个函数应该是sort,而不是limit。

例子:

db.testcollection.find().limit(3).sort({timestamp:-1}); 

你可能想要使用find选项: http://docs.meteor.com/api/collections.html#Mongo-Collection-find

db.collection.find({}, {sort: {createdAt: -1}, skip:2, limit: 18}).fetch();

查看查询:排序和自然顺序,http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order 以及游标方法下的sort() http://www.mongodb.org/display/DOCS/Advanced+Queries