Ohad
Ohad

Reputation: 1631

Regex with OR in Java - mongoDB

How can I create the following query:

Name LIKE "ABC" or "DFG" in java driver for mongoDB?

I know that for creating regex without "or" I do it in this way:

  BasicDBObject regexQuery = new BasicDBObject();
  regexQuery.put("name",
       new BasicDBObject("$regex", "ABC"));

Upvotes: 1

Views: 787

Answers (1)

enator
enator

Reputation: 2599

The normal | operator works on mongo.

This should do:

BasicDBObject regexQuery = new BasicDBObject();
regexQuery.put("name",
       new BasicDBObject("$regex", "ABC\\|DFG"));

If trying on mongo shell:

db.collection.find({name:/ABC|DFG/}).pretty()

Upvotes: 3

Related Questions