java - 正则表达式查询Mongodb $和运算符结果问题

标签 java regex mongodb mongodb-query

我有一个 MongoDB 集合,其中在单个文档中包含 6 个字段,例如:

{
     "_id" : ObjectId("59d0f2382043f72a443e6ec0"),
     "TranDate" : "2017-07-25T18:01:12+08:00",
     "TranCode" : "ActFLA_A01_01_",
     "TranValue" : "812.34",
     "Seq" : "71",
     "Configuration" : "10"
},
{
     "_id" : ObjectId("59d0f2332043f72a443e1397"),
     "TranDate" : "2017-07-25T18:01:12+08:00",
     "TranCode" : "ActFLA_A01_01_",
     "TranValue" : "87.34",
     "Seq" : "71",
     "Configuration" : "10"
},
{
     "_id" : ObjectId("59d0f2332043f72a443e1397"),
     "TranDate" : "2017-07-25T18:01:12+08:00",
     "TranCode" : "ActFLA_A01_01_",
     "TranValue" : "828.34",
     "Seq" : "71",
     "Configuration" : "10"
},
{
        "_id" : ObjectId("59d0f2342043f72a443e16be"),
        "TranDate" : "2017-07-25T00:45:02+08:00",
        "TranCode" : "ActFLA_A01_01_",
        "TranValue" : "0.00",
        "Seq" : "2",
        "Configuration" : "0"
}

这是我的查询代码:

Pattern regex = Pattern.compile("^2017-07-25"); 
Pattern regex2 = Pattern.compile("^ActFLA_A");
Pattern regex3 = Pattern.compile("^10");
DBObject clause1 = new BasicDBObject("TranDate", regex);  
DBObject clause2 = new BasicDBObject("TranCode", regex2);
DBObject clause3 = new BasicDBObject("Configuration", regex3);
BasicDBList and = new BasicDBList();
and.add(clause1);
and.add(clause2);
and.add(clause3);
DBObject query = new BasicDBObject("$and", and);

我只得到一个文档,但我期待 3 个文档,其中包含(字段)TranDate、TranCode、TranSeq 和配置,其值相同(TranValue 除外)。

最佳答案

该问题暗示您有兴趣退回满足以下所有条件的(三份)文件:

  • TranDate2017-07-25 开头
  • TranCodeActFLA_A 开头
  • 配置10开头

以下代码将满足该要求:

@Test
public void aQuery() {
    MongoClient mongoClient = new MongoClientFactory().create();

    MongoCollection<Document> collection = mongoClient.getDatabase("stackoverflow").getCollection("ors");

    Pattern regex = Pattern.compile("^2017-07-25");
    Pattern regex2 = Pattern.compile("^ActFLA_A");
    Pattern regex3 = Pattern.compile("^10");

    Bson clause1 = Filters.regex("TranDate", regex);
    Bson clause2 = Filters.regex("TranCode", regex2);
    Bson clause3 = Filters.regex("Configuration", regex3);

    Bson query = Filters.and(clause1, clause2, clause3);

    Assert.assertEquals(3, collection.count(query));

    // let's have a look at the matched docs ...
    FindIterable<Document> documents = collection.find(query);
    for (Document document : documents) {
        logger.info(document.toJson());
    }
}

鉴于 OP 中包含的示例文档,上述测试记录以下内容:

2017-10-01 17:31:59 [main] INFO  c.s.mongo.MongoClientTest - { "_id" : { "$oid" : "59d0f2382043f72a443e6ec0" }, "TranDate" : "2017-07-25T18:01:12+08:00", "TranCode" : "ActFLA_A01_01_", "TranValue" : "812.34", "Seq" : "71", "Configuration" : "10" }
2017-10-01 17:31:59 [main] INFO  c.s.mongo.MongoClientTest - { "_id" : { "$oid" : "59d0f2332043f72a443e1397" }, "TranDate" : "2017-07-25T18:01:12+08:00", "TranCode" : "ActFLA_A01_01_", "TranValue" : "87.34", "Seq" : "71", "Configuration" : "10" }
2017-10-01 17:31:59 [main] INFO  c.s.mongo.MongoClientTest - { "_id" : { "$oid" : "59d11663c26584cd8b7a0ee8" }, "TranDate" : "2017-07-25T18:01:12+08:00", "TranCode" : "ActFLA_A01_01_", "TranValue" : "828.34", "Seq" : "71", "Configuration" : "10" }

关于java - 正则表达式查询Mongodb $和运算符结果问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46514023/

相关文章:

mongodb - mgo go 服务器中打开的文件太多

java - 使用 Spring Security 获取通过 Active Directory 登录的用户的电子邮件地址

嵌入特殊字符的 url 出现 java.lang.IllegalArgumentException

python - RegEx Python - 我想编写 RegEx,通过它我可以将 { expression } 替换为 {% print expression %}

c# - 如何在 Mongo 驱动程序中为 "orderby"编写查询以供 C# 排序?

c# - 驱动程序连接到项目

java - 在EnvironmentPostProcessor中获取ServletContext

java - 在 Eclipse 中调试时实时更改变量?

javascript - 将正则表达式组解构为对象而不是数组

c# - 如何重写 Regex.Replace(由于异步 api)