1717

Case insensitive property mapping

Question:

When serializing a MongoDB document to a POCO is there any way to make properties map case insensitive? For example I'd like this document:

{ "id": "1" "foo": "bar" }

to map to this class:

public MyObj { public int Id {get; set;} public string Foo {get; set;} }

Answer1:

To do that I think you will have 2 options.

The first would be to write out a class map manually

BsonClassMap.RegisterClassMap<MyClass>(cm => { cm.AutoMap(); cm.GetMemberMap(c => c.Foo).SetElementName("foo"); });

The second would be to decorate your class with the following attributes

public class MyObj { [BsonElement("id")] public int Id { get; set; } [BsonElement("foo")] public string Foo { get; set; } }

The CSharp driver team have a good tutorial on serialization on the following link

<a href="http://docs.mongodb.org/ecosystem/tutorial/serialize-documents-with-the-csharp-driver/" rel="nofollow">http://docs.mongodb.org/ecosystem/tutorial/serialize-documents-with-the-csharp-driver/</a>

<h3>Update</h3>

I have just tried the following and this works for me, obviously I'm sure this is a much more simplified version of your code but taking a guess at how it might look.

I have registered the two class maps separately and added the BsonKnownType to the base class.

[BsonKnownTypes(typeof(GeoJSONObject))] public class Point { public string Coordinates { get; set; } } public class GeoJSONObject : Point { public string Type { get; set; } } static void Main(string[] args) { var cn = new MongoConnectionStringBuilder("server=localhost;database=MyTestDB;"); var settings = MongoClientSettings.FromConnectionStringBuilder(cn); var client = new MongoClient(settings); BsonClassMap.RegisterClassMap<Point>(cm => { cm.AutoMap(); cm.GetMemberMap(c => c.Coordinates).SetElementName("coordinates"); }); BsonClassMap.RegisterClassMap<GeoJSONObject>(cm => { cm.AutoMap(); cm.GetMemberMap(c => c.Type).SetElementName("type"); }); var result = client.GetServer() .GetDatabase("MyTestDB") .GetCollection("MyCol") .Find(Query.EQ("type", BsonValue.Create("xxxx"))); }

Recommend

  • Mapping a BsonArray to a List in a POCO
  • How do I disable a control in an ExtJS grid?
  • Reportlab - Add two Paragraphs into one table cell
  • C#: Find out result type of arithmetic operations in code (e.g. int + double = double)
  • Close open InfoBubble when open a new (gMap v3)
  • Reset default matplotlib colormap values after using 'set_under' or 'set_over'
  • How to use a context manager inside a decorator and how to pass an object created in decorator to de
  • Generate setup.exe for ClickOnce deployment from command line using MSBuild
  • setMaxForRoute does not work in ThreadSafeClientConnManager
  • RadioButtonFor not selecting value in editor templates
  • Custom colourmaps in matplotlib
  • Export Excel (.xlsx extension) file with includes responsive table data in SAPUI5
  • Embed Barcode in C# PDF Library
  • HL7 accelerator error: Schema not found (after upgrade to BizTalk 2013)
  • JUnit: selectively not run a few test cases as default
  • How can I know what was the latest changes made to a branch?
  • Load unistall icon from exe file in the inno setup
  • Replacing the Translator service in Symfony 3
  • Implementing a friends relationship in Symfony3 with Doctrine
  • Selecting max of a sum of two columns
  • SSRS: Can you force textboxes on same row to have different height?
  • Pdflatex run crashes when executed from Java (on Windows)
  • Matplotlib change colormap tab20 to have three colors
  • Why can't I use constants in [SuppressMessage(…)] together with StyleCop?
  • Replace repeating delimiters in a text file with an alternate character
  • How to set the height of a PDF header?
  • How to mention namespace in XSLT file when the namespaces are dynamic?
  • Every second request using Apache HTTPClient fails
  • How can I get the concrete type of a generic type variable using mirrors in Dart?
  • Deserialize XML string to complex type
  • Using constants or global variables in 3 tier console application
  • Checking a play current mode makes an error occur
  • Spark (Scala) Writing (and reading) to local file system from driver
  • pip in virtualenv gets ConnectTimeoutError
  • pyodbc doesn't report sql server error
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?