By default indexes you create in MongoDB are case sensitive.
How to create a case insensitive index using the C# client?
By default indexes you create in MongoDB are case sensitive.
How to create a case insensitive index using the C# client?
You have to specify the collation when creating the index, and set a collation strength for it.
Here is some sample code:
var mongoConnectionString = "your-mongo-connection-string";
var mongoClient = new MongoClient(mongoConnectionString);
var database = mongoClient.GetDatabase("Inventory");
var productCollection = database.GetCollection<Product>("Products");
var createIndexModel =
new CreateIndexModel<Product>(Builders<Product>.IndexKeys.Ascending(i => i.ProductReference),
new CreateIndexOptions
{
Collation = new Collation("en", false, CollationCaseFirst.Off, CollationStrength.Secondary)
});
productCollection.Indexes.CreateOne(createIndexModel);
Last Edit | August 31, 2022 |
Created | August 31, 2022 |
Views | 71 |
Tags |
MongoDB
C#
|
Do you like this website?
Bookmark it and come back here to write a post yourself when you run into something shareable.
Early collaborators will get to own a part of the project (5 to 20%)... >> read more
1 Comment
A warning so you don't lose an hour like I did: Atlas Serverless instances do not support collation, it simply does not create the index if you try to use them.