mongodb 查询mongodb中的字符串类型Date
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9314271/
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
Query the string type Date in mongodb
提问by user1214124
This is my query in mongodb
这是我在 mongodb 中的查询
db.order.find ({"ublExtensions.ublExtensions.extensionContent.Metadata
.ModificationTime": "2012-02-04T01:58:27.861Z"});
It is fetching the result only for the particular time zone. The modificationTime is changing on a daily basis. I have a requirement where I need to pick the data which are updated y'day...means (sysdate -1 in sql) one day data.
它仅获取特定时区的结果。修改时间每天都在变化。我有一个要求,我需要选择 y'day 更新的数据......意味着(sql 中的 sysdate -1)一天数据。
How can I achieve this in mongodb? Please note the my Date is a String Type and I have a constraint where I can't change that. Someone suggested to use the ISODate.
如何在 mongodb 中实现这一目标?请注意,我的日期是字符串类型,并且我有一个无法更改的约束。有人建议使用 ISODate。
Please help in resolving this issue.
请帮助解决此问题。
回答by jdi
Mongodb stores its date objects in a bson format like: {$date: 1329415205151}
Mongodb 以 bson 格式存储其日期对象,例如:{$date: 1329415205151}
If you decide to store it in a string format, then it is the client-side responsibility to filter and process this value as mongo treats it like a string. You can convert your strings to date objects by referring to this other SO question: How do I convert a property in MongoDB from text to date type?
如果您决定以字符串格式存储它,那么过滤和处理此值是客户端的责任,因为 mongo 将其视为字符串。您可以通过参考另一个 SO 问题将字符串转换为日期对象:How do I convert a property in MongoDB in MongoDB from text to date type?
Its been widely recommended to either store all your dates in UTC, or, a consistent timezone possibly related to the local datacenter, and then convert your date values to the proper local timezone on the client.
广泛建议将您的所有日期存储在 UTC 中,或者使用可能与本地数据中心相关的一致时区,然后将您的日期值转换为客户端上正确的本地时区。
You can store whatever Date value you want. The valueof the date and the formatof the date are two separate issues. If your constraints require you to store that string-based date format in the document, it would be recommended to also store a $date object as well at the time of update.
您可以存储您想要的任何日期值。日期的值和日期的格式是两个不同的问题。如果您的约束要求您在文档中存储基于字符串的日期格式,则建议在更新时也存储一个 $date 对象。
回答by Ghasfarost
I would recommend storing your dates as Date or ISODate on MongoDB since it would ease your life :)
我建议将您的日期存储为 MongoDB 上的 Date 或 ISODate ,因为它会减轻您的生活:)
If not possible, then on the client side (programming language specific driver) you should be able to parse the string to the appropiate Date type.
如果不可能,那么在客户端(特定于编程语言的驱动程序),您应该能够将字符串解析为适当的日期类型。
However, if you need your query to run on mongo shell, you could follow an strategy similar as described at Mongo Cookbook
但是,如果您需要在 mongo shell 上运行查询,您可以遵循类似于Mongo Cookbook 中描述的策略
1) Creating a var with today's date and yesterday's date:
1)用今天的日期和昨天的日期创建一个var:
>var today = new Date();
>today
ISODate("2012-11-26T22:12:03.870Z")
>var yesterday = new Date()
>yesterday.setDate(today.getDate() - 1)
>yesterday
ISODate("2012-11-25T22:12:03.870Z")
2) Form a query for finding documents within a 24 hours period
2) 形成查询以在 24 小时内查找文档
>db.order.find( { "ublExtensions.ublExtensions.extensionContent.Metadata.ModificationTime" : { $gte : yesterday, $lt : today } } )
回答by Sudhanshu Gaur
Why can't you just use $gt
query and for comparing time you can use Date.now()
and from that Date.now()
you can use previous date, like if current date is "2017-05-16T02:08:48.419+05:30"
then you will write the query
为什么你就不能使用$gt
查询和比较时,您可以使用Date.now()
并从Date.now()
您可以使用以前的日期一样,如果当前的日期是"2017-05-16T02:08:48.419+05:30"
,那么你将编写查询
db.collection.find({"createdAt": { $gte : new ISODate("2017-05-15T02:08:48.419+05:30") }})