如何获取流星mongodb的N个最新记录?
我知道我可以用普通的mongodb:db.foo.find().sort({_id:1});这样做,所以我认为这可以用meteor:collection.find({chatroom: Session.get("room")}, {sort: {_id:1}, limit: N })来做。
但这只返回一些“随机”文档。我猜它们是_id值最低的10个记录,比如_id= aaaaa和_id= aaaab。
这里我漏掉了什么?在普通的mongodb中它超简单吗?!
发布于 2013-06-26 19:59:13
尝试使用mongoDB的$natural排序说明符。
collection.find({chatroom: Session.get("room")}, {sort: {$natural : 1}, limit: N });自然顺序是数据库在磁盘上存储文档的顺序。通常是插入顺序。
我通常使用date_created值进行排序。因为在对现有文档执行update操作时,自然顺序有时会发生变化。
发布于 2013-07-23 22:15:42
我也被限制了(我认为这是一个Meteor bug)。我最终编写了这个函数:
Template.content.messages = function () {
var items = Messages.find({}, {sort: {timestamp : 1} }).fetch();
return items.slice(-15);
}时间戳字段定义如下:
timestamp: new Date().getTime()https://stackoverflow.com/questions/17318292
复制相似问题