javascript - 如何将正常时间戳转换为 BSON 时间戳?

标签 javascript node.js mongodb timestamp bson

我正在使用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-bsonmongodb/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/

相关文章:

javascript - contentDocument.documentURI 替代IE6和IE7?

javascript - 如何在 HTML5 数据属性中添加单引号和双引号字符串

mongodb - 在 cl-mongo 中实现 MongoDB SASL 身份验证

node.js - Mongodb 数据模型策略指导/建议

jQuery anchor 点击, "this", "e.target"- 返回 url

javascript - 让单个 javascript 文件在多个 html 页面上工作

php - memcache 是否适合将用户详细信息从 Apache 传递到 Node.js

node.js - 使用 tooltipster 带有滚动条的工具提示

Node.js 模块从 MongoDB 数据库获取数据

javascript - 收到错误,指出声明的变量未定义