Mongoexport在日期范围内使用$ gt和$ lt约束 [英] Mongoexport using $gt and $lt constraints on a date range

查看:151
本文介绍了Mongoexport在日期范围内使用$ gt和$ lt约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下mongoexport调用从我的mongodb获取某一天的订单:

I am trying to get the orders made for a certain day from my mongodb using the following mongoexport call:

mongoexport --db store --collection user_data --query{'order.created_order':{$ gt:ISODate(2013-02-05T00:00:00.000Z),$ lt:ISODate(2013-02-06T00:00:00.000 Z)},'order.status':'付费'}--out ordersfeb6.json

但我目前正在经历以下错误:

but I am currently experiencing the following error:

Thu Feb  7 18:33:43 Assertion: 10340:Failure parsing JSON string near: 'order.cre
0x56a223 0x5712e5 0x53e0f7 0x53e21e 0x8b7739 0x524f2b 0x5258a3 0x7fa7b77bd76d 0x525975 
mongoexport(_ZN5mongo15printStackTraceERSo+0x23) [0x56a223]
mongoexport(_ZN5mongo11msgassertedEiPKc+0xc5) [0x5712e5]
mongoexport(_ZN5mongo8fromjsonEPKcPi+0x377) [0x53e0f7]
mongoexport(_ZN5mongo8fromjsonERKSs+0xe) [0x53e21e]
mongoexport(_ZN6Export3runEv+0x489) [0x8b7739]
mongoexport(_ZN5mongo4Tool4mainEiPPc+0x72b) [0x524f2b]
mongoexport(main+0x23) [0x5258a3]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed) [0x7fa7b77bd76d]
mongoexport() [0x525975]
assertion: 10340 Failure parsing JSON string near: 'order.cre

从这个问题 mongoexport JSON解析错误我知道javascript用于评估mongo查询的某些部分。我想知道:$ gt和$ lt运算符是否需要javascript,这是我的问题吗?如果没有,我不确定我的查询有什么问题,我们将非常感谢任何建议。谢谢你的阅读!

From this question mongoexport JSON parsing error I know that javascript is used to evaluate some parts of the mongo queries. I was wondering: do the $gt and $lt operators require javascript and is that my problem? If not, I'm not sure what is wrong with my query and any suggestions would be greatly appreciated. Thank you for reading!

推荐答案

这里的问题是你如何表示日期,它们需要以日期传递类型和纪元格式。请改为尝试:

The issue here is how you are representing the dates, they need to be passed in as Date types and in epoch format. Try this instead:

mongoexport --db store --collection user_data --query '{"order.created_order":{$gt:new Date(1360040400000),$lt:new Date(1360990800000)}, "order.status" : "paid"}' --out ordersfeb6.json

如果您想将ISODate转换为epoch,只需在shell中调用date,如下所示:

If you are looking to convert ISODate to epoch, just call date in the shell, something like this:

> new Date(2013,01,16)*1
1360990800000

然后验证:

> new Date(1360990800000)
ISODate("2013-02-16T05:00:00Z")

更新:正如 imcaptor 的评论中所述,月份为零(0 = Jan,11 = Dec)在 Date 构造函数中,不是大多数人期望的东西,而且容易忘记。我在上面的示例中通过了01并获得了2月的日期,正如您在ISODate中可以看到的那样。

Update: As noted in the comments by imcaptor, the Month is zero based (0 = Jan, 11 = Dec) in the Date constructor, not something most will expect, and easy to forget. I passed in 01 in the example above and got a February date, as you can see in the ISODate from the verification.

这篇关于Mongoexport在日期范围内使用$ gt和$ lt约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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