[JAVA-552] What doesn't JSONParser support the /{regex}/i syntax for regular expressions? Created: 12/Apr/12 Updated: 11/Sep/19 Resolved: 13/Apr/12 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | API |
| Affects Version/s: | 2.7.2 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Blocker - P1 |
| Reporter: | Manish | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
I need to implement regular expression based search on a collection that exists on my production system. Lets say the collection is called User and the search needs to take place on the Name field. Two use cases are required: 1. User provides a keyword and all User instances are returned where Name contains the provided keyword. This translates nicely into the query: { name : { $regex : "simp", $options : "i" }} The Java code works well for this use case. 2. User provides a keyword and all User instances are returned where Name does not contain the provided keyword. This needs to translate into the following query: { name : { $not : /simp/i }} since $not is not supported with $regex. However, I cannot pass a string value containing this query to JSONParser because it throws a parse exception. I understand that they query is not valid JSON but works fine when executed directly from the Mongo console. Therefore, the question is, why doesn't JSONParser correctly translate the second query string into a valid DBObject? |
| Comments |
| Comment by Jeffrey Yemin [ 13/Apr/12 ] |
|
Manish, thank you for the suggestion, but we're going to have to have to leave it the way it is. |
| Comment by Scott Hernandez (Inactive) [ 12/Apr/12 ] |
|
As you said, it is not valid json. The mongo javascript shell is a javascript interpreter not just a json parser – there are many things you can do there which aren't valid json like declaring functions or writing loops for example. The mongodb json parser works fine with this '{$not:{ $regex : "simp", $options : "i" }}' and it can be sent to the server without error; the regex gets converted to a Pattern object. In general you should not be using strings to build queries like this. |