【问题标题】:Mongoexport using $gt and $lt constraints on a date rangeMongoexport 在日期范围内使用 $gt 和 $lt 约束
【发布时间】:2013-02-07 18:44:29
【问题描述】:

我正在尝试使用以下 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,这是我的问题吗?如果没有,我不确定我的查询有什么问题,任何建议将不胜感激。感谢您的阅读!

【问题讨论】:

    标签: javascript mongodb mongoexport


    【解决方案1】:

    这里的问题是您如何表示日期,它们需要以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 转换为纪元,只需在 shell 中调用 date,如下所示:

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

    然后去验证:

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

    更新: 正如 imcaptor 在 cmets 中所指出的,在 Date 构造函数中,月份是从零开始的(0 = 1 月,11 = 12 月),这不是大多数人所期望的,并且容易忘记。我在上面的示例中通过了 01 并获得了 2 月的日期,正如您在验证中的 ISODate 中看到的那样。

    【讨论】:

    • @亚当。感谢你的回答。将问题的标题更改为与 $lt $gt 运算符相反的日期(正如您的答案明确指出的那样)是否合适?如果是这样的话,我会更快地找到你的答案。
    • 月份从0开始,一定要小心。
    • 感谢@imcaptor - 我在答案中添加了一个(归因的)更新以使其清楚
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 2014-12-31
    • 2014-01-07
    相关资源
    最近更新 更多