我应该使用什么格式存储用于nodejs中使用的mongodb查询的时间戳?

时间:2021-10-08 00:10:51

I am currently using python to preprocess my data into JSON and insert my data into the collection via nodejs: collection.insert(data, {safe:true}, function(err, result) {}); My queries will be executed using nodejs as well.

我目前正在使用python将数据预处理为JSON,并通过nodejs: collection将数据插入到集合中。插入(data, {safe:true}, function(err, result) {});我的查询也将使用nodejs执行。

I previously used python datetime.datetime to format my timestamps, and this is how my JSON is structured:

我以前使用过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
  },
  ...
 ]

I was trying to query based on the timestamp, and according to a lot of posts such as this Create an ISODate with pyMongo, I am supposed to use ISODate for my query. So one of my attempts is:

我试图基于时间戳进行查询,根据许多类似这样的文章,我应该使用ISODate来查询pyMongo。我的一个尝试是:

  collection.findOne({ timestamp: new ISODate("2016-08-02T20:33:23.786Z")})

This one says that ISODate is undefined. So based on this post I changed my query to:

这个说ISODate没有定义。因此,基于这篇文章,我将我的查询改为:

  collection.findOne({ timestamp: new Date("2016-08-02T20:33:23.786Z")})

This one says record not found. How should I store my timestamps into JSON? Is new Date("2016-08-02T20:33:23.786Z") the right way to retrieve my instance? Also, when mongodb query timestamps, will it look at exact hours and minutes as well? I want to find the exact year, month, day, hour, minute and second just like how my timestamp is stored.

这个说没有找到记录。如何将时间戳存储到JSON中?新日期(“2016-08-02t20:23.786z”)是检索实例的正确方法吗?此外,当mongodb查询时间戳时,它是否也会查看确切的时间和分钟?我想找到确切的年份、月、日、小时、分钟和秒,就像我的时间戳是如何存储的一样。

1 个解决方案

#1


1  

The following 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 it further clarifies the internal values:

下面的答案表明,最好的方法是使用JavaScript日期(也通过ISODate构造),它是Mongo的原生版本。深入研究BSON文档,它进一步阐明了内部价值:

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.

BSON日期是一个64位整数,表示自Unix纪元以来的毫秒数(1970年1月1日)。这导致在过去和未来大约2.9亿年的时间范围具有代表性。

So the short answer is: no matter which language you store it from, just store that 64bit integer number of milliseconds, which you will be able to read natively. I would expect that value to be in UTC, both written and read.

因此,简短的回答是:无论您使用哪种语言存储它,只需存储64位的整数毫秒数,您将能够以本机方式读取它。我期望这个值在UTC中,包括写的和读的。

The following answer also tells you how to convert your python datetime object to milliseconds since epoch.

下面的答案还将告诉您如何将您的python datetime对象转换为自纪元以来的毫秒数。

#1


1  

The following 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 it further clarifies the internal values:

下面的答案表明,最好的方法是使用JavaScript日期(也通过ISODate构造),它是Mongo的原生版本。深入研究BSON文档,它进一步阐明了内部价值:

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.

BSON日期是一个64位整数,表示自Unix纪元以来的毫秒数(1970年1月1日)。这导致在过去和未来大约2.9亿年的时间范围具有代表性。

So the short answer is: no matter which language you store it from, just store that 64bit integer number of milliseconds, which you will be able to read natively. I would expect that value to be in UTC, both written and read.

因此,简短的回答是:无论您使用哪种语言存储它,只需存储64位的整数毫秒数,您将能够以本机方式读取它。我期望这个值在UTC中,包括写的和读的。

The following answer also tells you how to convert your python datetime object to milliseconds since epoch.

下面的答案还将告诉您如何将您的python datetime对象转换为自纪元以来的毫秒数。