{"id":88,"date":"2010-05-02T22:59:13","date_gmt":"2010-05-03T04:59:13","guid":{"rendered":"https:\/\/www.chrisedwards.dreamhosters.com\/blog\/2010\/05\/02\/accessing-mongodb-via-c\/"},"modified":"2010-06-14T23:06:03","modified_gmt":"2010-06-15T05:06:03","slug":"accessing-mongodb-via-c","status":"publish","type":"post","link":"http:\/\/architester.com\/blog\/2010\/05\/02\/accessing-mongodb-via-c\/","title":{"rendered":"Accessing MongoDB via C#"},"content":{"rendered":"<p>Last week we looked at setting up a MongoDB instance, and working with it through the Mongo shell. This week, we look at accessing it through the C# driver.<\/p>\n<h1>Getting the Driver<\/h1>\n<p>There are several .NET drivers available for MongoDB. You can check out the list <a href=\"http:\/\/www.mongodb.org\/display\/DOCS\/C+Sharp+Language+Center\" target=\"_blank\">here<\/a>. I chose to use the <a href=\"http:\/\/github.com\/samus\/mongodb-csharp\" target=\"_blank\">mongodb-csharp<\/a> driver. It seems to to have the widest feature set, and the most support. To use it, you can either build it from the source, or download the binaries directly <a href=\"http:\/\/github.com\/samus\/mongodb-csharp\/downloads\" target=\"_blank\">here<\/a>.<\/p>\n<h1>Using the Driver<\/h1>\n<p>To use the driver, simply create a new project and add a reference to it. For our sample, create a new console application. Then right click on references, choose add reference, and find the <span style=\"font-family: 'Courier New';\">MongoDB.Driver.dll<\/span> and add it.<\/p>\n<p><a href=\"https:\/\/www.chrisedwards.dreamhosters.com\/blog\/wp-content\/uploads\/2010\/05\/image.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"https:\/\/www.chrisedwards.dreamhosters.com\/blog\/wp-content\/uploads\/2010\/05\/image_thumb.png\" border=\"0\" alt=\"image\" width=\"244\" height=\"223\" \/><\/a><\/p>\n<p>After adding the reference we are ready to write code. Here is a very simple snippet of code to test out Mongo. This is the full content of <span style=\"font-family: 'Courier New';\">Program.cs<\/span>.<\/p>\n<pre class=\"csharp\">using System;\r\nusing MongoDB.Driver;\r\n\r\nnamespace MongoTest\r\n{\r\n\tinternal class Program\r\n\t{\r\n\t\tprivate static void Main( string[] args )\r\n\t\t{\r\n\t\t\t\/\/ Connect to the mongo instance.\r\n\t\t\tvar mongo = new Mongo();\r\n\t\t\tmongo.Connect();\r\n\r\n\t\t\t\/\/ Use the myorders database.\r\n\t\t\tDatabase db = mongo.GetDatabase( \"myorders\" );\r\n\r\n\t\t\t\/\/ Get the orders collection.\r\n\t\t\tIMongoCollection orders = db.GetCollection( \"orders\" );\r\n\r\n\t\t\t\/\/ Create a new order.\r\n\t\t\tvar order = new Document();\r\n\t\t\torder[\"orderAmount\"] = 57.22;\r\n\t\t\torder[\"customerName\"] = \"Elmer Fudd\";\r\n\r\n\t\t\t\/\/ Add the new order to the mongo orders colleciton.\r\n\t\t\torders.Insert( order );\r\n\t\t\tConsole.WriteLine( string.Format( \"Inserted: {0}\", order ) );\r\n\r\n\t\t\t\/\/ Create a specification to query the orders collection.\r\n\t\t\tvar spec = new Document();\r\n\t\t\tspec[\"customerName\"] = \"Elmer Fudd\";\r\n\r\n\t\t\t\/\/ Run the query.\r\n\t\t\tDocument result = orders.FindOne( spec );\r\n\t\t\tConsole.WriteLine( string.Format( \"Found: {0}\", result ) );\r\n\r\n\t\t\tConsole.WriteLine(\"Press Enter to Exit.\");\r\n\t\t\tConsole.ReadLine();\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<p>The comments should explain most of what is happening. One thing worthy of note is the <span style=\"font-family: 'Courier New';\">Document<\/span> class. The <span style=\"font-family: 'Courier New';\">Document<\/span> class is much like a dictionary. Remember that MongoDB does not have a static schema. Therefore a dictionary serves the purpose well since it is dynamic by nature.<\/p>\n<p>There are other drivers (such as <a href=\"http:\/\/github.com\/atheken\/NoRM\" target=\"_blank\">NoRM<\/a>) out there that will map a static C# class into MongoDB much like NHibernate or another ORM, however, then you lose some of the benefit of the dynamic schema, however, depending on your needs, this may be acceptable.<\/p>\n<h1>Starting up MongoDB<\/h1>\n<p>Note that to use this, you need to have the instance of MongoDB up and running. You can follow the instructions from <a href=\"https:\/\/www.chrisedwards.dreamhosters.com\/blog\/2010\/04\/25\/test-driving-mongodb\/\" target=\"_blank\">last week\u00e2\u20ac\u2122s post<\/a> to get it installed. If its already installed, just open a command prompt, go to the bin folder in the install directory (in my case <span style=\"font-family: 'Courier New';\">C:\\mongodb\\bin<\/span>), and execute the command <span style=\"font-family: 'Courier New';\">mogod.<\/span><\/p>\n<p><a href=\"https:\/\/www.chrisedwards.dreamhosters.com\/blog\/wp-content\/uploads\/2010\/05\/image1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"https:\/\/www.chrisedwards.dreamhosters.com\/blog\/wp-content\/uploads\/2010\/05\/image_thumb1.png\" border=\"0\" alt=\"image\" width=\"522\" height=\"160\" \/><\/a><\/p>\n<h1>Running the Code<\/h1>\n<p>Now you are ready to run the application. Doing so gives the following output.<\/p>\n<blockquote><p><span style=\"font-family: 'Lucida Console'; font-size: xx-small;\">Inserted: { &#8220;_id&#8221;: &#8220;4bde57114076a60500000001&#8221;, &#8220;orderAmount&#8221;: 57.22, &#8220;customerName&#8221;: &#8220;Elmer Fudd&#8221; }<\/p>\n<p>Found: { &#8220;_id&#8221;: &#8220;4bde41cd4076a60df0000001&#8221;, &#8220;orderAmount&#8221;: 57.22, &#8220;customerName&#8221;: &#8220;Elmer Fudd&#8221; }<\/span><\/p>\n<p><span style=\"font-family: 'Lucida Console'; font-size: xx-small;\"> <\/span><span style=\"font-family: 'Lucida Console'; font-size: xx-small;\">Press Enter to Exit.<\/span><\/p><\/blockquote>\n<p>Next week we\u00e2\u20ac\u2122ll either dive a bit deeper into the driver\u00e2\u20ac\u2122s api, or look at the autosharding capabilities of MongoDB. Not sure which yet. See you then!<\/p>\n<p>-Chris<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week we looked at setting up a MongoDB instance, and working with it through the Mongo shell. This week, we look at accessing it through the C# driver. Getting the Driver There are several .NET drivers available for MongoDB. You can check out the list here. I chose to use the mongodb-csharp driver. It [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,31,24,23],"tags":[79,30,81,80],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-mongodb","category-nosql","tag-net","tag-csharp","tag-mongodb","tag-nosql"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":5,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":174,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/posts\/88\/revisions\/174"}],"wp:attachment":[{"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}