对于 nodejs 中使用的 mongodb 查询,我应该使用什么格式在 JSON 中存储时间戳?
What format should I use to store timestamp in JSON for mongodb queries used in nodejs?
我目前正在使用 python 将我的数据预处理为 JSON 并通过 nodejs 将我的数据插入到集合中:collection.insert(data, {safe:true}, function(err, result) {});
我的查询也将使用 nodejs 执行。
我以前使用 python datetime.datetime
来格式化我的时间戳,这就是我的 JSON 的结构:
[{
"timestamp" : "2016-08-02 20:30:02",
"detail" : blah blah blah
},{
"timestamp" : "2016-08-02 20:33:23",
"detail" : blah blah blah
},
...
]
我试图根据时间戳进行查询,根据很多帖子,例如 Create an ISODate with pyMongo,我应该使用 ISODate
进行查询。所以我的尝试之一是:
collection.findOne({ timestamp: new ISODate("2016-08-02T20:33:23.786Z")})
这个说 ISODate 是未定义的。因此,基于此 post 我将查询更改为:
collection.findOne({ timestamp: new Date("2016-08-02T20:33:23.786Z")})
这个说找不到记录。我应该如何将我的时间戳存储到 JSON 中? new Date("2016-08-02T20:33:23.786Z")
是检索我的实例的正确方法吗?此外,当 mongodb 查询时间戳时,它是否也会查看准确的小时和分钟?我想找到确切的年、月、日、小时、分钟和秒,就像我的时间戳的存储方式一样。
下面answer suggests that the best way is to use the JavaScript Date
(constructed also via ISODate
), which is native to Mongo. Digging into the BSON documentation进一步阐明了内部价值观:
BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). This results in a representable date range of about 290 million years into the past and future.
所以简短的回答是:无论您使用哪种语言存储它,只需存储 64 位整数毫秒数,您就可以本机读取它。我希望该值以 UTC 格式显示,包括写入和读取。
following answer 还告诉您如何将 python datetime
对象转换为纪元以来的毫秒数。
我目前正在使用 python 将我的数据预处理为 JSON 并通过 nodejs 将我的数据插入到集合中:collection.insert(data, {safe:true}, function(err, result) {});
我的查询也将使用 nodejs 执行。
我以前使用 python datetime.datetime
来格式化我的时间戳,这就是我的 JSON 的结构:
[{
"timestamp" : "2016-08-02 20:30:02",
"detail" : blah blah blah
},{
"timestamp" : "2016-08-02 20:33:23",
"detail" : blah blah blah
},
...
]
我试图根据时间戳进行查询,根据很多帖子,例如 Create an ISODate with pyMongo,我应该使用 ISODate
进行查询。所以我的尝试之一是:
collection.findOne({ timestamp: new ISODate("2016-08-02T20:33:23.786Z")})
这个说 ISODate 是未定义的。因此,基于此 post 我将查询更改为:
collection.findOne({ timestamp: new Date("2016-08-02T20:33:23.786Z")})
这个说找不到记录。我应该如何将我的时间戳存储到 JSON 中? new Date("2016-08-02T20:33:23.786Z")
是检索我的实例的正确方法吗?此外,当 mongodb 查询时间戳时,它是否也会查看准确的小时和分钟?我想找到确切的年、月、日、小时、分钟和秒,就像我的时间戳的存储方式一样。
下面answer suggests that the best way is to use the JavaScript Date
(constructed also via ISODate
), which is native to Mongo. Digging into the BSON documentation进一步阐明了内部价值观:
BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). This results in a representable date range of about 290 million years into the past and future.
所以简短的回答是:无论您使用哪种语言存储它,只需存储 64 位整数毫秒数,您就可以本机读取它。我希望该值以 UTC 格式显示,包括写入和读取。
following answer 还告诉您如何将 python datetime
对象转换为纪元以来的毫秒数。