[JAVA-914] $in query with $regex doesn't work Created: 01/Aug/13  Updated: 11/Jun/18  Resolved: 04/Dec/13

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: nirav Assignee: Trisha Gee
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 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



 Comments   
Comment by Rizwan Ishtiaq [ 11/Jun/18 ]

Hi

I am not able to access #FREE-30992 (what is the reason for that?)

I am facing some what same problem.

 

{'alternativeNames' : { '$in' : [?0] } }//Where ?0 will be replaced by string "/\\bSCHA\\b/i"

 

I am passing string because $regex is not allowed with $in operator. I reckon it is not treating it as a regex

 

Comment by Jeffrey Yemin [ 04/Dec/13 ]

Closing this, as it it not an issue with the driver.

Comment by nirav [ 01/Aug/13 ]

The workaround I am using doesn't seems to be efficient. Regular expression with mulitple "|" seems to performing worse than $in query. I can send explain() if interested. I have also tried $Or query instead of $in, which also seems to be performing worse than $in

Generated at Thu Feb 08 08:53:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.