, free & fully virtual. June 9th - 10th. Register Now

Case insensitive query with Java driver

How to search case insensitive with java driver?

I created an index:

db.user.createIndex({ userName: 1 },{ collation: {locale: 'en', strength: 2}});

I can search successfully using shell:

db.names.find( { userName: "betsy" } ).collation( { locale: 'en' } )

This returns expected 1 record

I cannot find similar method collation on the java driver api.


com.mongodb.client.MongoCollection's find() method returns a FindIterable; it has a collation‚Äč() method where you can specify the collation for the find method returned cursor object.

db.collection.find() returns a cursor, and the cursor object has the collation() method and corresponds to the above mentioned Java method .

Thanks for the help. Getting expected results now using code below.

val collation = Collation.builder()

val list = collection.find(and(eq("familyId", item.familyId), eq("userName", item.userName)))