[CSHARP-819] DOCS: Case insensative string serialization in .NET driver Created: 11/Sep/13  Updated: 02/Feb/18  Resolved: 02/Feb/18

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 1.8.2
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Michael Kennedy Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

C# Driver


Backwards Compatibility: Fully Compatible

 Description   

I would like an attribute which controls string serialization to / from Mongo at the C# layer. Something like

[BsonStringOptions(UseLowerCase = true)]

The motivation: Here's some feedback from a new app I'm working on in C#.

We are coming from SQL Server where most string comparisons are case insensitive. Obviously MongoDB is case sensitive.

There are plenty of fields we want to continue case insensitive comps on. For example, usernames, emails, urls, etc. We are fine to just store them all lowercase. But having the serialization engine help ensure this would help.

It would be great if there was an attribute that would help with this (kind of like [BsonDateTimeOptions(Kind = DateTimeKind.Local)] for UTC vs Local time).

So I'd like to offer a new feature idea:

class Model {
[BsonStringOptions(UseLowerCase = true)]
public string Username

{get; set;}

}

Where when this object is saved, it automatically converts to lowercase. I can do this in code, but it's a pain and potentially error prone.



 Comments   
Comment by Michael Kennedy [ 16/Sep/13 ]

Thanks Craig. That's good to know. I'll see what I can whip up. It'd be nice to see it part of the official driver eventually.

Cheers,
Michael

Comment by Craig Wilson [ 11/Sep/13 ]

Hi Michael,
Good suggestion. For now, it would be relatively straightforward to create a custom string serializer to do just this. You can use the existing string serializer as a template: https://github.com/mongodb/mongo-csharp-driver/blob/master/MongoDB.Bson/Serialization/Serializers/StringSerializer.cs.

You obviously don't want this serializer used for all strings. For this, you can create an attribute yourself by inheriting from Attribute and implementing IBsonMemberMapAttribute. You can then switch the serializer for this custom string one out inside the Apply method of the attribute.

Generated at Wed Feb 07 21:37:55 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.