我正在使用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和 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
关于javascript - 如何将正常时间戳转换为 BSON 时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976839/