此 Meteor 代码尝试查找集合中的最后一个文档。
.find({userId: this.userId}, {sort: {createdAt: -1}, limit: 1});
但由于所有文档都是按时间顺序排列的,我想删除createdAt字段,所以一旦被“删除”,是否有一种“不昂贵”的方法来返回最后输入的文档?谢谢
{userId: 'someId', info: 'someInfo'}
myCol.findLast().info; <--- my wish list
$最后(聚合) https://docs.mongodb.org/manual/reference/operator/aggregation/last/在文档中首先对其他内容进行排序
edit
尝试利用斯蒂芬·伍兹提出的想法;
服务器.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});
});
我将做出这样的假设:昂贵的你的意思是执行时间。在这种情况下,您想要一个createdAt
字段,二级索引createdAt
,并使用findOne()
陈述。创建索引createdAt
对于您的收藏,请执行以下操作:
myCol._ensureIndex({ createdAt: -1 });
然后在您的发布中:
Meteor.publish('myCol', function () {
return MyCol.find({userId: this.userId}, { sort: { createdAt: -1 } });
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)