为什么自动转换为ISODate? [英] why automatically converting to ISODate?

查看:641
本文介绍了为什么自动转换为ISODate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在外壳上,我尝试了测试数据库

On shell I tried on test db

post = {"title" : "My Blog Post", "content" : "Here's my blog post.", ... "date" : new Date()}

但是当我尝试使用命令来检索它

but when I tried to retrieve it using command

db.blog.find();

它给了我输出

{ "_id" : ObjectId("4f13fdc4af1aaf90a686f8ae"), "title" : "My Blog Post", "content" : "Here's my blog post.", "date" : ISODate("2012-01-16T10:35:54.985Z") }

为什么显示日期为ISODate?我不能只保存日期,而不能在JS中返回什么新的Date()?

why its showing date as ISODate?? Can I not save date just what new Date() is return in js?

推荐答案

ISODate是Shell的帮助程序函数,用于包装javascript的Date构造函数.调用ISODate()new Date()应该会产生完全相同的Date对象,它将以不同的方式打印.

ISODate is the shell's helper function to wrap javascript's Date constructor. Calling ISODate() and new Date() should produce the exact same Date object, it will just be printed differently.

> var date = new Date(2012,01,16,10,35,54,985)
> var isodate = ISODate("2012-01-16T10:35:54.985Z")
> date.constructor == isodate.constructor
true

> date.constructor
function Date() {
    [native code]
}
> isodate.constructor
function Date() {
    [native code]
}

但是:

> date.valueOf()
1329384954985
> isodate.valueOf()
1326710154985
> 

但是,如果您使用完全相同的毫秒来构造它们,那么它们是相等的:

however if you use the exact same millisecond to construct them then they are equal:

> date = new Date(isodate.valueOf())
> print(date)
Mon Jan 16 2012 11:35:54 GMT+0100 (CET)
> date.valueOf() == isodate.valueOf()
true

这篇关于为什么自动转换为ISODate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆