问题描述
是否有可能在 Meteor 中保留插入顺序或设置可靠的时间戳,因为如果未指定排序,MongoDB 不保证按插入顺序返回项目,文档的 _id 是随机生成的,并在插入时手动设置时间戳将取决于客户的时钟?
Is it possible yet, to preserve insertion order or set reliable timestamps in Meteor given that MongoDB doesn't guarantee to return items in insertion order if no sort is specified, a document's _id is randomly generated and setting a timestamp manually on insertion would depend upon the client's clock?
推荐答案
我建议一个方法.
Meteor.methods({
addItem: function (doc) {
doc.when = new Date;
return Items.insert(doc);
}
});
虽然客户端将在本地运行它并将 when
设置为它自己的当前时间,但服务器的时间戳优先并传播到所有订阅的客户端,包括原始客户端.您可以对 doc.when
进行排序.
While the client will run this locally and set when
to its own current time, the server's timestamp takes priority and propagates to all subscribed clients, including the original client. You can sort on doc.when
.
我们可能会添加用于自动设置时间戳的钩子,作为文档验证和权限的一部分.
We'll probably add hooks for setting timestamps automatically as part of document validations and permissions.
这篇关于如何在 Meteor 中使用时间戳并保留插入顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!