Mongodb:如果比较运算符起作用,为什么将字符串日期转换为ISOdate? [英] Mongodb : Why convert string date to ISOdate if comparison operators work?

查看:136
本文介绍了Mongodb:如果比较运算符起作用,为什么将字符串日期转换为ISOdate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类型的文件:

{ 
  "_id" : ObjectId("538d64a11ca6e50941fda4d9"), "_id" : "538d518e20b8fd642e0000e8", 
  "posts" : "some stuff", "date" : "2014-06-02"
}

使用比较运算符输入字符串日期(不是Mongodb ISODate)有效:

Using comparison operators for a string date (Not the Mongodb ISODate) works:

> collection.find({"date": {"$gte": "2014-06-02"}})

那么为什么我们(还要)将字符串日期转换为ISODate?

So why shall we (bother to) convert string dates to an ISODate then?

推荐答案

使用MongoDB BSON Date类型而不是字符串的最大好处是只能使用aggregate

Probably the biggest advantage of using the MongoDB BSON Date type instead of a string is that you can only use the aggregate Date operators with BSON Date values.

但是,如果您不需要对数据进行任何汇总,那么使用可排序的字符串格式表示日期就可以了,而且通常更简洁,因为您不需要处理与时区相关的显示问题.

But if you don't need to do any aggregation of your data, using a sortable string format to represent dates is fine and often cleaner as you don't need to deal with time zone related display problems.

这篇关于Mongodb:如果比较运算符起作用,为什么将字符串日期转换为ISOdate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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