Queries invoking the following string methods: equals(), matches(), startsWith(), endsWith() can be evaluated in-database using $regexp operator provided that:
1. The method is invoked on a field,
2. The method's parameter is a parameter expression.
It's worth noting that MongoDB will handle a query with startsWith() efficiently using an index, if one exists.
For substring search, the most efficient way is to use matches() predicate, escaping string literal with Java Pattern.quote(). If a case-insensitive query is desirable, escaped string should be prefixed with "(?i)" regexp modifier.
Even though substring search cannot be speed up by an index, having this evaluated in-database still allows a part of query to use index and prevents in-memory query evaluation.