Javascript 通过 mongo shell 将 Date() 插入到 Mongodb

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27480134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:12:05  来源:igfitidea点击:

Inserting Date() in to Mongodb through mongo shell

javascriptmongodbmongodb-query

提问by Logicbomb

I am inserting document through following query:

我通过以下查询插入文档:

db.collection.insert(  
{  
      date: Date('Dec 12, 2014 14:12:00')  
})  

But it will give me an error.

但它会给我一个错误。

How can I insert a date in my collection without getting an error?

如何在不出错的情况下在我的集合中插入日期?

回答by chridam

You must be getting a different error as the code above will result in the Date()method returning the current date as a string, regardless of the arguments supplied with the object. From the documentation: JavaScript Date objects can only be instantiated by calling JavaScript Dateas a constructor: calling it as a regular function (i.e. without the newoperator) will return a string rather than a Dateobject; unlike other JavaScript object types, JavaScript Date objects have no literal syntax.

您必须得到一个不同的错误,因为上面的代码将导致该Date()方法以字符串形式返回当前日期,而不管对象提供的参数如何。来自文档JavaScript Date 对象只能通过将 JavaScriptDate作为构造函数调用来实例化:将其作为常规函数调用(即没有new运算符)将返回字符串而不是Date对象;与其他 JavaScript 对象类型不同,JavaScript Date 对象没有文字语法。

You might want to try this instead to get the correct date, bearing in mind that the month parameter of JavaScript's Date constructor is 0-based:

你可能想试试这个来获得正确的日期,记住 JavaScript 的 Date 构造函数的月份参数是基于 0 的:

var myDate = new Date(2014, 11, 12, 14, 12);
db.collection.insert({ "date": myDate });

回答by Neil Lunn

JavaScript date objects can be a funny thing. Depending on how you actually supply the arguments to instantiate them you get different results.

JavaScript 日期对象可能是一件有趣的事情。根据您实际提供参数来实例化它们的方式,您会得到不同的结果。

For example, some might suggest you try this:

例如,有些人可能会建议你试试这个:

var myDate = new Date(2014, 11, 12, 14, 12)

Which seems fine, but there is a problem.

这看起来不错,但有一个问题。

You see that some forms of instantiating a Dateobject in JavaScript use the "local" timezone when creating the date. Others use the UTC or "universal" time zone to a set standard. This is also the "standard" that MongoDB expects, and is generally accepted as best practice for your application to store dates in this way. Do any conversions in your code, away from the data store. This ensures you can deal with multiple locales without issue.

您会看到Date在 JavaScript中实例化对象的某些形式在创建日期时使用“本地”时区。其他人使用 UTC 或“通用”时区来设定标准。这也是 MongoDB 期望的“标准”,并且被普遍接受为应用程序以这种方式存储日期的最佳实践。在代码中进行任何转换,远离数据存储。这确保您可以毫无问题地处理多个语言环境。

So what you should be doing is this:

所以你应该做的是:

var date = new Date("2014-12-11T14:12:00Z")

There is also a "helper" in the MongoDB shell that handles this pretty much in the same way, but more specific to the syntax:

在 MongoDB shell 中还有一个“助手”,它以几乎相同的方式处理这个问题,但更具体到语法:

var date = new ISODate("2014-12-11T14:12:00Z")

This produces a UTC Date value that stores correctly as expected. You should always deal with UTC when storing dates in MongoDB.

这会生成一个按预期正确存储的 UTC 日期值。在 MongoDB 中存储日期时,您应该始终处理 UTC。