1

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

1 Answer 1

3

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()
Sign up to request clarification or add additional context in comments.

1 Comment

one can also use Pattern.compile("ABC\\|DFG")

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.