在mongodb中查询字符串类型Date [英] Query the string type Date in mongodb

查看:1167
本文介绍了在mongodb中查询字符串类型Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在mongodb中的查询

This is my query in mongodb

db.order.find ({"ublExtensions.ublExtensions.extensionContent.Metadata
                .ModificationTime": "2012-02-04T01:58:27.861Z"});

它仅在特定时区获取结果.修改时间每天都在变化.我有一个要求,我需要选择更新了y'day的数据...意味着(sql中的sysdate -1)一天的数据.

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.

如何在mongodb中实现这一目标?请注意,我的日期是一个字符串类型,我有一个约束,我不能更改它.有人建议使用ISODate.

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以bson格式存储其日期对象,例如:{$ date:1329415205151}

Mongodb stores its date objects in a bson format like: {$date: 1329415205151}

如果您决定以字符串格式存储它,则客户端负责过滤和处理此值,因为mongo将其视为字符串.您可以通过参考另一个SO问题将字符串转换为日期对象:

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?

广泛建议您将所有日期存储在UTC中,或者存储可能与本地数据中心相关的一致时区,然后将日期值转换为客户端上的正确本地时区.

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.

您可以存储所需的任何日期"值.日期的和日期的格式是两个独立的问题.如果您的约束要求您在文档中存储该基于字符串的日期格式,则建议在更新时也存储一个$ date对象.

You can store whatever Date value you want. The value of the date and the format of 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.

这篇关于在mongodb中查询字符串类型Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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