-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?

Konstantin Yovkov
  • 60,548
  • 8
  • 97
  • 143
Randomize
  • 7,970
  • 18
  • 69
  • 122
  • @Philipp That would be a duplicate if only it actually mentioned how to do it in JAVA – Sammaye Nov 21 '13 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. – WiredPrairie Nov 21 '13 at 18:07

1 Answers1

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);
idbentley
  • 4,126
  • 3
  • 31
  • 48