【问题标题】:Using jongo runcommand for regex使用 jongo runco​​mmand 进行正则表达式
【发布时间】:2018-01-08 06:03:34
【问题描述】:

以下示例如何使用 jongo runco​​mmand 执行正则表达式:

       Command command = jongo.runCommand("{" +
            "find : 'sales', filter : {$and : [{date : {$gte : #}},"
            + "{date : {$lte : #}}, { name: { $regex: /^TEST/ } } ]}  "
            + "}", d1,d2);


    List<MyDate> t = command.throwOnError().field("cursor").as(MyDate.class);

它向我抛出了这个异常:

   {find : 'sales', filter : {$and : [{date : {$gte : #}},{date : {$lte : #}}, { name: { $regex: /^TEST/ } } ]}  }

 at org.jongo.query.BsonQueryFactory.createQuery(BsonQueryFactory.java:233)
at org.jongo.Command.<init>(Command.java:43)
at org.jongo.Jongo.runCommand(Jongo.java:69)
at com.mtrojahn.boot.JongoTest.start(JongoTest.java:152)
at com.mtrojahn.boot.JongoTest.main(JongoTest.java:50)
    Caused by: com.mongodb.util.JSONParseException: 




  Regards
  Kris

【问题讨论】:

  • $regex: '/^TEST/'替换正则表达式

标签: mongodb jongo


【解决方案1】:

发现答案用 $options 和 $regex 重构了查询

像这样:

   {find : 'sales', filter : {$and : [{date : {$gte : #}},{date : {$lte : #}}, { 'name' : { '$regex' : "^Test" , "$options" : "i"} ]}  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 2020-02-18
    相关资源
    最近更新 更多