{"id":924,"date":"2013-12-18T20:48:23","date_gmt":"2013-12-18T20:48:23","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=924"},"modified":"2013-12-18T20:48:23","modified_gmt":"2013-12-18T20:48:23","slug":"handling-case-sensitive-mapping-from-mongodb-to-a-poco","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/handling-case-sensitive-mapping-from-mongodb-to-a-poco\/","title":{"rendered":"Handling case-sensitive mapping from MongoDB to a POCO"},"content":{"rendered":"<p>So the convention appears to be to use camel case for column\/field names within MongoDB. For example if we create an entry such as <em>db.cds.Update({<strong>artist<\/strong>:&#8221;Alice Cooper&#8221;})<\/em>.<\/p>\n<p>In C# the convention is for properties, for example, to be written in Pascal case. So we&#8217;d have something like <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class CD\r\n{\r\n   public ObjectId Id { get; set; }\r\n   public string Artist { get; set; }\r\n   public string Title { get; set; }\r\n   public string Category { get; set; }\r\n}\r\n<\/pre>\n<p>So obviously MongoDB has a field name <em>artist<\/em> and we need to map it to the property name &#8220;Artist&#8221;.<\/p>\n<p>To handle this we can use the BsonElement in the MongoDB.Bson.Serialization.Attributes namespace, as per<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class CD\r\n{\r\n   public ObjectId Id { get; set; }\r\n   &#x5B;BsonElement(&quot;artist&quot;)]\r\n   public string Artist { get; set; }\r\n   &#x5B;BsonElement(&quot;title&quot;)]\r\n   public string Title { get; set; }\r\n   &#x5B;BsonElement(&quot;category&quot;)]\r\n   public string Category { get; set; }\r\n}\r\n<\/pre>\n<p>or we can set up the mappings using the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nBsonClassMap.RegisterClassMap&lt;CD&gt;(cm =&gt;\r\n{\r\n   cm.AutoMap();\r\n   cm.GetMemberMap(c =&gt; c.Artist).SetElementName(&quot;artist&quot;);\r\n   cm.GetMemberMap(c =&gt; c.Title).SetElementName(&quot;title&quot;);\r\n   cm.GetMemberMap(c =&gt; c.Category).SetElementName(&quot;category&quot;);\r\n});\r\n<\/pre>\n<p>Note: we do not need to setup the Id field to any mapping as this appears to be mapped based upon it&#8217;s type.<\/p>\n<p>A class map may only be registered once, we can use BsonClassMap.IsClassMapRegistered if need be to ensure this.<\/p>\n<p>More information can be found at <a href=\"http:\/\/docs.mongodb.org\/ecosystem\/tutorial\/serialize-documents-with-the-csharp-driver\/\" title=\"Serialize Documents with the CSharp Driver\" target=\"_blank\">Serialize Documents with the CSharp Driver<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So the convention appears to be to use camel case for column\/field names within MongoDB. For example if we create an entry such as db.cds.Update({artist:&#8221;Alice Cooper&#8221;}). In C# the convention is for properties, for example, to be written in Pascal case. So we&#8217;d have something like public class CD { public ObjectId Id { get; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,22,62],"tags":[],"class_list":["post-924","post","type-post","status-publish","format-standard","hentry","category-c","category-database","category-mongodb"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=924"}],"version-history":[{"count":8,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/924\/revisions"}],"predecessor-version":[{"id":932,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/924\/revisions\/932"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}