这段Meteor代码试图找到集合中的最后一个文档。
.find({userId: this.userId}, {sort: {createdAt: -1}, limit: 1});
但是由于所有文档都按时间顺序排列,我想删除createdAt字段,这样一旦删除,是否有一种“不昂贵”的方式只返回最后输入的文档?谢谢。
{userId: 'someId', info: 'someInfo'}
myCol.findLast().info; <--- my wish list
$last(aggregation)在文档中首先进行排序等操作。
编辑
尝试利用Stephen Woods提供的想法;
server.js
MyCol._ensureIndex({createdAt: -1});
MyCol.before.insert(function (userId, doc) {
doc.userId = userId;
createdAt = Date.now();
});
Meteor.publish('myCol', function () {
return MyCol.findOne({userId: this.userId});
});
_id
:{sort: {_id: -1}, limit: 1}
。这是一个唯一索引,所以应该很便宜。 - Alex Blex_id
没有按照时间顺序排序。同时您可以使用$sort
进行.findOne()
。 - Michel Floyd_id
排序时_id
的顺序根本不匹配createdDate
。 - Michel FloydObjectId
。Meteor 使用随机包生成包含字母数字的_id
。 - Michel Floyd