node.js - 获取代表现在(当前时间)的时间戳用于查询mongodb oplog

标签 node.js mongodb bson mongodb-oplog

oplog 有一个名为 ts 的字段,如下所示:

{"ts":"6533707677506207745","t":2,"h":"-7325657776689654694", ...}

我想查询oplog,如下所示:

db.oplog.rs.find({ts:{$gt:x}});

如何生成代表现在的时间戳?现在 30 秒之前怎么样?

如果我这样做:

const x = new Timestamp();

我收到一条错误消息:

enter image description here

生成时间戳的正确方法是什么?如何使用正确的 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/

相关文章:

node.js - 在 Azure 上部署 Hexo.. 不知道我错过了什么

javascript - 从本地机器访问 firebase 数据库

ruby - mongo db中的可尾游标超时

java - mongoDB:数组中不存在的文档的 $inc

BSON id 生成器的 C# 实现?

arrays - 在 node.js 和 socket.io 中发送数组时出现错误

node.js - NodeJS : Get File, 不是缓冲区

ruby-on-rails - 从 MongoDB 1.8 升级到 2.4 时如何解决 MongoDB/PCRE “symbol lookup error”

java - 如何为resteasy实现bson生产者/消费者

python - 检查 python 中的字符串是否有效 BSON