Mongoexport在日期范围内使用$ gt和$ lt约束

时间:2013-02-07 18:44:30

标签: javascript mongodb mongoexport

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

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.000Z")}, 'order.status':'paid'}" --out ordersfeb6.json

但我目前遇到以下错误:

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 parsing error我知道javascript用于评估mongo查询的某些部分。我想知道:$ gt和$ lt运算符是否需要javascript,这是我的问题吗?如果没有,我不确定我的查询有什么问题,我们将非常感谢任何建议。谢谢你的阅读!

1 个答案:

答案 0 :(得分:53)

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

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,如下所示:

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

然后验证:

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

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