-1

How can I write the equivalent of this query for Mongo DB in Java:

db.mydata.find({$where: "this.fields.name.toLowerCase().indexOf('ar') > 0"})

?

The query is used to find all names that include "AR" (martin, mark, etc.).

I am using QueryBuilder at the moment, but it doesn't support that kind of query format.

Any idea?

2
  • @Philipp That would be a duplicate if only it actually mentioned how to do it in JAVA Commented Nov 21, 2013 at 16:11
  • Realize that this is going to search every document in the collection if you don't use an anchored regex search (and have an indexed field). Slow. Commented Nov 21, 2013 at 18:07

1 Answer 1

6

An option is to use a Regex instead.

i.e.

Pattern regex = Pattern.compile("ar",  Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
DBObject query = new DBObject("fields.name", regex);
Cursor result = myCollection.find(query);
Sign up to request clarification or add additional context in comments.

1 Comment

you are the man! thank you! Just a little correction: new BasicDBObject("fields.name", regex);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.