简体   繁体   English

使用$ regex查询mongodb java $

[英]mongodb java $in query with $regex

I am trying to write $in query with $regex in mongo+java. 我想在mongo + java中使用$ regex编写$ in查询。 It's not working in mongo shell either. 它也不适用于mongo shell。 What I mean is I don't get any results but no query parse error either. 我的意思是我没有得到任何结果,但也没有查询解析错误。 Here's the final query I got from Java Debugger at the line where I say collection.find(finalQuery) ` 这是我从Java Debugger获得的最后一个查询,在我说的是collection.find(finalQuery) `

{ "$and" : [ 
        { "$or" : 
                [ { "country" : "united states"}]} , 
        { "businesses" : 
                { "$in" : [ { "$regex" : "^.*cardinal.*health.*$"} , { "$regex" :      "^.*the.*hartford.*$"}]}
         }
        ]
}

Java Code snipet for Above query: Set businesses = new HashSet(); Java Code snipet for Above query: Set businesses = new HashSet();

for(String st: srchTerms){

businesses.add(Pattern.compile("^"+st.trim()+"$"));

}
srchTermQuery.append("businesses", new BasicDBObject("$in", businesses ));

` However, following query works in mongo shell but I don't know how to write it into java: `但是,以下查询在mongo shell中工作,但我不知道如何将其写入java:

{"registering_organization" : {"$in":[/^.*cardinal.*health.*$/,/^.*the.*hartford.*$/]}}

Java Code add double quotes around regex if we try to define it as a string. 如果我们尝试将其定义为字符串,Java代码会在正则表达式周围添加双引号。

The behavior you're seeing might be a bug, however as an alternative you can write your query like this 您所看到的行为可能是一个错误,但作为替代方案,您可以像这样编写查询

Pattern pattern = Pattern.compile("(^aaa$)|(^bbb$)");
srchTermQuery.append("businesses", pattern);

Not pretty but it seem to do the trick 不漂亮,但它似乎做了伎俩

You're not going to be able to convert: 你无法转换:

{"businesses" : {"$in":[/^.*cardinal.*health.*$/,/^.*the.*hartford.*$/]}}

directly into a Java regex. 直接进入Java正则表达式。 This is not a bug, it's because the Java driver uses $regex format when creating regex queries to avoid any ambiguity. 这不是错误,这是因为Java驱动程序在创建正则表达式查询时使用$ regex格式以避免任何歧义。

The $regex documentation states that $ regex文档说明了这一点

db.collection.find( { field: /acme.*corp/ } );
db.collection.find( { field: { $regex: 'acme.*corp' } } );

So your Java-generated query of: 所以你的Java生成的查询:

{ "businesses" : { "$in" : [ { "$regex" : "^.*cardinal.*health.*$"}, 
                             { "$regex" : "^.*the.*hartford.*$"}]}
                 }
}

is exactly equivalent of the query you were trying to convert: 与您尝试转换的查询完全等效:

{"businesses" : {"$in": [/^.*cardinal.*health.*$/,
                         /^.*the.*hartford.*$/]}
                }
}

In summary, the Java you've written is already the correct way to convert the query you wanted. 总之,您编写的Java已经是转换所需查询的正确方法。 I've run it in my own test and it returns the expected results. 我在自己的测试中运行它并返回预期的结果。

Perhaps if you included some sample documents that you expect to be returned by the query we could help further? 也许如果您包含一些您希望查询返回的示例文档,我们可以进一步提供帮助吗?

I had a need to list all keys beginning with a specified string. 我需要列出以指定字符串开头的所有键。 The following worked for me in CLI: 以下在CLI中为我工作:

db.crawlHTML.count({"_id": /^1001/})

The following was the Java implementation: 以下是Java实现:

public List<String> listKeysLike(DB mongoDB, String likeChars) throws Exception {

    DBCollection dbCollection = this.getHTMLCollection(mongoDB, TESTPROD);
    List<String> keyList = new ArrayList<String>();

    BasicDBObject query = new BasicDBObject();      
    String queryString = "^" + likeChars.trim() ;   // setup regex
    query.put("_id", java.util.regex.Pattern.compile(queryString));  
    DBCursor cursor = dbCollection.find(query);

    while (cursor.hasNext()) {      // _id used as the primary key
        BasicDBObject obj = (BasicDBObject) cursor.next();
        String tempString = obj.getString("_id");
        keyList.add(tempString);
    }       // while

    return keyList;
}

NB: The "TESTPROD" just tells me which of two databases I should be using. 注意:“TESTPROD”告诉我应该使用哪两个数据库。

你必须使用mongodb正则表达式而不是将它放在一个字符串中

db.somecollection.find({records: {$in: [/.*somestring.*/]}})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM