20

I have to run this query in Java

 db.Users.find({"name": /^ind/i})

My Java code is

Document findQuery = new Document();
findQuery.append("name", Pattern.compile("/^"+name+"/i"));
FindIterable<Document> iterable = db.getCollection("Users").find(findQuery);

It is not returning any data, I think the above java code is converting

> /^ind/i into "/^ind/i"

Thanks in advance.

Edit:

Based on stribizhev suggestion updated the query and its worked

db.Users.find({"name": {"$regex": /^ind/, "$options": "i"}})

Java code

Document regQuery = new Document();
regQuery.append("$regex", "^(?)" + Pattern.quote(name));
regQuery.append("$options", "i");

Document findQuery = new Document();
findQuery.append("name", regQuery);
FindIterable<Document> iterable = db.getCollection("Users").find(findQuery);
5
  • 2
    In Java, do not use regex delimiters. Try "^(?i)"+Pattern.quote(name) instead of "/^"+name+"/i". Commented Sep 22, 2015 at 10:19
  • Thanks, it worked so basically "^(?)"+Pattern.quote("ind") converted value to ^(?)\Qind\E Commented Sep 22, 2015 at 10:31
  • Yes, that is true. I posted the suggestion as an answer. Commented Sep 22, 2015 at 10:34
  • Please specify which import you use. import java.util.regex.Pattern; Commented Sep 26, 2017 at 6:56
  • Something similar worked for me - findQuery.append("name", new BsonRegularExpression(regexLiteral)); Commented Jan 6, 2021 at 6:55

4 Answers 4

9

You cannot use regex delimiters in Java Pattern.compile, as there are other means in Java (e.g. flags) to do the same.

To enforce case-insensitive search, use inline modifier (?i). So, use "^(?i)"+Pattern.quote(name) instead of "/^"+name+"/i".

Pattern.quote just escapes all regex metacharacters so that they were treated as literals (same as \Q...\E).

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, exactly so I updated the mongo query as well to use this db.Users.find({"name": {"$regex": /^ind/, "$options": "i"}}) Java code is Document regQuery = new Document(); regQuery.append("$regex", "^(?)" + Pattern.quote(name)); regQuery.append("$options", "i");
You may add this code you end up with to the question, at the end.
8

I guess there is a more elegant way to do that in Java, by using the provided Filters in the MongoDB Java Driver (version 3 and above):

Document query = new Document("equipment","gloves");

//whatever pattern you need. But you do not need the "/" delimiters
String pattern = ".*" + query.getString("equipment") + ".*";

//find(regex("field name", "pattern", "options"));
collection.find(regex("equipment", pattern, "i"));

Comments

8

You could use Pattern -and Filters-

Pattern regex = Pattern.compile("ind", Pattern.CASE_INSENSITIVE);
Bson filter = Filters.eq("name", regex);

The object Pattern has some other flags, as you can see here

Comments

3

Most of the answers here are not working for me (maybe not up-to-date MongoDB Java driver). Here is what worked for me:

Document regexQuery = new Document();
regexQuery.append("$regex", ".*" + Pattern.quote(searchTerm) + ".*");
BasicDBObject criteria = new BasicDBObject("name", regexQuery);
DBCursor cursor = collection.find(criteria);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.