Details
-
Bug
-
Resolution: Done
-
Major - P3
-
None
-
None
-
None
-
None
Description
I am trying to write $in query with $regex in mongo+java. It's not working in mongo shell either. 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)
{ "$and" : [
|
{ "$or" :
|
[ { "country" : "united states"}]} ,
|
{ "businesses" :
|
{ "$in" : [ { "$regex" : "^.*cardinal.*health.*$"} , { "$regex" : "^.*the.*hartford.*$"}]}
|
}
|
]
|
}
|
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:
{"registering_organization" : {"$in":[/^.*cardinal.*health.*$/,/^.*the.*hartford.*$/]}}
Java Code add double quotes around regex if we try to define it as a string.
For workaround, I can do as someone suggested on StackOverflow using Or in my regex.
Pattern pattern = Pattern.compile("(^aaa$)|(^bbb$)");
srchTermQuery.append("businesses", pattern);
http://stackoverflow.com/questions/17984750/mongodb-java-in-query-with-regex