I am facing issues related to case-insensitive sort while using the C# MongoDB Driver 2.19. Specifically, the output I am getting is not in the proper sort order as required with case-insensitive sort.
I have tried to implement case-insensitive sort by using the Collation feature. However, as I understand it, Collation is not supported in the latest version of MongoDB 2.19 for C#.
I am using the following code to perform a case-insensitive sort:
private SortDefinition<Channel> GetSortDefinition(string sortOrder, string sortBy)
{
SortDefinition<Channel> sortDefinition;
var collation = new Collation("en", strength: CollationStrength.Primary, caseLevel: false);
switch (sortOrder)
{
case "Ascending":
{
sortDefinition = sortBy switch
{
"ChannelName" => Builders<Channel>.Sort.Ascending(x => x.ChannelName, collation: collation),
"CreatedDate" => Builders<Channel>.Sort.Ascending(x => x.CreatedDate),
"RecipeCookbookUpdatedDate" => Builders<Channel>.Sort.Ascending(x => x.RecipeCookbookUpdatedDate),
_ => Builders<Channel>.Sort.Ascending(x => x.CreatedDate),
};
}
break;
case "Descending":
{
sortDefinition = sortBy switch
{
"ChannelName" => Builders<Channel>.Sort.Descending(x => x.ChannelName, collation: collation),
"CreatedDate" => Builders<Channel>.Sort.Descending(x => x.CreatedDate),
"RecipeCookbookUpdatedDate" => Builders<Channel>.Sort.Descending(x => x.RecipeCookbookUpdatedDate),
_ => Builders<Channel>.Sort.Descending(x => x.CreatedDate),
};
}
break;
default:
sortDefinition = Builders<Channel>.Sort.Descending(x => x.CreatedDate);
break;
}
return sortDefinition;
}
Unfortunately, this code is not giving me the expected results. I am receiving the following error: “The best overload for ‘Descending’ does not have a parameter named 'collation” and “The best overload for ‘Ascending’ does not have a parameter named 'collation”.
Could you please help me understand what I am doing wrong here? How can I achieve case-insensitive sort in the C# MongoDB Driver 2.19? I would appreciate any guidance on how to resolve this issue.
Thank you for your time and support.