Java Mongodb regex query

how to execute mongodb query in java
mongodb query multiple criteria java
mongodb like query java
mongodb regex search on integer value
mongoose regex
mongodb text search regex
mongodb string replace regex
mongodb query java

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);

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).

MongoDB Regular Expressions Example, 2. MongoDB Regular Expression Example. 2.1 $regex method in the Mongo database. In the Mongo universe, the $regex operator allows the user to get those documents from a collection where a string matches a specified pattern. 2.2 Practical usage. 2.3 Regular Expression Optimization. This allows MongoDB to construct a “range” from that prefix and only match against those values from the index that fall within that range. A regular expression is a “prefix expression” if it starts with a caret (^) or a left anchor (\A), followed by a string of simple symbols.


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"));

com.mongodb.client.model.Filters.regex java code examples, @param fieldName the field name * @param pattern the pattern * @return the filter * @mongodb.driver.manual reference/operator/query/regex $regex */ public​  Yes I can reproduce this problem on mongodb 2.4.5 via shell. I suggest file a bug on their JIRA site: jira.mongodb.org – gerrytan Aug 1 '13 at 4:18


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, like you can see here

Java MongoDB : Query document – Mkyong.com, Java MongoDB : Query document. Find() examples. 1.1 Get first matched document only. Find() and Comparison. 2.1 Get all documents where number = 5 . find() and Logical. 3.1 $and example – get documents where number = 2 and name = 'mkyong-2' . find() and Regex. Find document with regular expression pattern. Full Example. The regex operator in MongoDB is used to search for specific strings in the collection. The following example shows how this can be done. Let's assume that we have our same Employee collection which has the Field names of "Employeeid" and "EmployeeName". Let' also assume that we have the following documents in our collection.


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);

$regex, Provides regular expression capabilities for pattern matching strings in queries. MongoDB uses Perl compatible regular expressions (i.e. “PCRE” ) version 8.42  MongoDB - Regular Expression Regular Expressions are frequently used in all languages to search for a pattern or word in any string. MongoDB also provides functionality of regular expression for string pattern matching using the $regex operator. MongoDB uses PCRE (Perl Compatible Regular Expression) as regular expression language.


How to Use the LIKE Query in MongoDB using Java, Regular expressions are used for pattern matching, which is basically to print each document which is returned by the query in a better way. You need to pass an instance of a Java RegEx (java.util.regex.Pattern): BasicDBObject q = new BasicDBObject(); q.put("name", java.util.regex.Pattern.compile(m)); dbc.find(q); This will be converted to a MongoDB regex when sent to the server, as well as any RegEx flags.


MongoDB Regular Expression (Regex) with Examples, MongoDB regex tutorial- MongoDB regular expression for pattern $regex operator, $option keyword, regex for array element, Query  In case if you are using Spring-java below mentioned is the way you can make it search in case-insensitive manner. public List<Discussion> searchQuestionByText(String qText){ Query query = new Query(); query.addCriteria(Criteria.where("discussionName").regex(qText,"i")); return mongoTemplate.find(query, Discussion.class); }


MongoDB Regular Expression ($option & $regex) with Examples , MongoDB - Regular Expression - Regular Expressions are frequently used in all The following regex query searches for all the posts containing string  Browse other questions tagged java mongodb mongodb-query mongo-java-driver or ask your own question. The Overflow Blog Learning to work asynchronously takes time