Skip to content

Instantly share code, notes, and snippets.

@Koboo
Last active October 5, 2021 06:46
Show Gist options
  • Save Koboo/8694a238e895d2622cbc646ecc3e6dc9 to your computer and use it in GitHub Desktop.
Save Koboo/8694a238e895d2622cbc646ecc3e6dc9 to your computer and use it in GitHub Desktop.
MongoDB Java Driver Filter for equals ignore case / case insensitive
import com.mongodb.client.model.Filters;
import org.bson.conversions.Bson;
import java.util.regex.Pattern;
public class MongoFilters {
public static Bson eqIgn(String fieldName, String value) {
String patternString = new StringBuilder("(?i)^").append(value).append("$").toString();
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
return Filters.regex(fieldName, pattern);
}
public static Bson notEqIgn(String fieldName, String value) {
return Filters.not(eqIgn(fieldName, value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment