Last active
October 5, 2021 06:46
-
-
Save Koboo/8694a238e895d2622cbc646ecc3e6dc9 to your computer and use it in GitHub Desktop.
MongoDB Java Driver Filter for equals ignore case / case insensitive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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