如何将我的正常时间戳转换为 BSON 时间戳?

How can I convert my normal timestamp into a BSON timestamp?

我正在使用 https://www.npmjs.com/package/mongodb

mongodb 已经把我的时间戳变成了 BSON(我知道这是正常的)

我的时间戳是 641912491,在数据库中它看起来像这样 1457904327000.000000

我有一个文档,其中包含一个名为 date_in_the_past 的键,它以 BSON 形式保存我的时间戳。

Note: I am fully aware that the mongodb _id key holds a date but it is of no use to me in this scenario as, that would be the date when the document was written to the db (not the date in the past that I am looking for)

如何将我的普通时间戳转换为 BSON 时间戳?

我试图理解 mongodb/js-bson and mongodb/js-bson/blob/master/lib/bson/timestamp.js 但我不知道该怎么做。

我想做的事的例子:

var past=timestampToBSON(641912491);
db.collection('docs').find({date_in_the_past:past}).limit(1).toArray(function(e,r){});

如果它存储为 bson 时间戳对象,你可以这样做

var Timestamp = require('mongodb').Timestamp; 

var past = Timestamp(641912491,1);
db.collection('docs').find({date_in_the_past:past})

db.collection('docs').find({date_in_the_past: Timestamp(641912491,1)})

这里有更多信息和示例 https://docs.mongodb.org/manual/reference/bson-types/#timestamps