oplog 有一个名为 ts 的字段,如下所示:
{"ts":"6533707677506207745","t":2,"h":"-7325657776689654694", ...}
我想查询oplog,如下所示:
db.oplog.rs.find({ts:{$gt:x}});
如何生成代表现在的时间戳?现在 30 秒之前怎么样?
如果我这样做:
const x = new Timestamp();
我收到一条错误消息:
生成时间戳的正确方法是什么?如何使用正确的 ts 值查询 oplog?
以下是时间戳的文档: http://mongodb.github.io/node-mongodb-native/core/api/Timestamp.html
但我无法弄清楚低数字/高数字是什么。
最佳答案
该时间戳是 UNIX 纪元的秒数,而 Date() 是毫秒。所以...
db.oplog.rs.find({ts:{$gt:Timestamp((new Date().getTime()-30000)/1000,0)}})
我们在这里做的是获取 30000 毫秒前的当前时间,将其除以 1000 得到秒数,然后向 Timestamp 函数添加(需要的)第二个参数。
编辑: 当然,如果您需要精确的 Timestamp() 值,则将 (new Date().getTime()-30000)/1000 的小数部分填充为第二个参数。
var x=(new Date().getTime()-30000)/1000;
var y=Math.floor(x);
var z=Math.round((x-y)*1000);
db.oplog.rs.find({ts:{$gt:Timestamp(y,z)}})
关于node.js - 获取代表现在(当前时间)的时间戳用于查询mongodb oplog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49331526/