{"id":907,"date":"2013-12-17T23:29:11","date_gmt":"2013-12-17T23:29:11","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=907"},"modified":"2013-12-17T23:29:11","modified_gmt":"2013-12-17T23:29:11","slug":"creating-a-simple-database-in-mongodb-with-c","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-a-simple-database-in-mongodb-with-c\/","title":{"rendered":"Creating a simple database in MongoDB with C#"},"content":{"rendered":"<p>This is quick post on getting up and running with MongoDB using the &#8220;Official MongoDB C# driver&#8221; and both creating a database and adding some data to it.<\/p>\n<ol>\n<li>Using Nuget, add the &#8220;Official MongoDB C# driver&#8221; from 10gen to your project<\/li>\n<li>Add the following using clauses\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing MongoDB.Bson;\r\nusing MongoDB.Driver;\r\n<\/pre>\n<\/li>\n<li>\nCreate a POCO object to represent the data you want to persist. The key thing to remember is to add a property of type <em>ObjectId<\/em> without this we&#8217;ll get and exception stating &#8220;No IdGenerator found&#8221;.<\/p>\n<p>So an example POCO might look like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class Person\r\n{\r\n   public ObjectId Id { get; set; }\r\n   public string FirstName { get; set; }\r\n   public string LastName { get; set; }\r\n   public int Age { get; set; }\r\n}\r\n<\/pre>\n<\/li>\n<li>\nNow we need to connect to the server and create\/use a database. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMongoClient client = new MongoClient();\r\nMongoServer server = client.GetServer();\r\nMongoDatabase db = server.GetDatabase(&quot;MyDatabase&quot;);\r\n<\/pre>\n<p>Obviously the first two lines create a mongo client and then gets access to the server. The line <em>server.GetDatabase(&#8220;MyDatabase&#8221;)<\/em> will get the database (if it exists) but also create a database if it doesn&#8217;t exist. <\/p>\n<p><em>Note: if you are creating a database using GetDatabase it will not exist until you actually store data in it.<\/em>\n<\/li>\n<li>\nNext we&#8217;re going to assume we want to store a collection of employees (a collection of Person objects). So we want to get the collection of &#8220;employees&#8221;. Like the creating of the database, if no employees currently exist we still get a collection object which we can then save data to.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMongoCollection&lt;Person&gt; collection = db.GetCollection&lt;Person&gt;(&quot;employees&quot;);\r\n<\/pre>\n<\/li>\n<li>\nLet&#8217;s now create a Person object ready for adding to the collection and ultimately to the database. <\/p>\n<p>Create the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nPerson p = new Person\r\n{\r\n   Id = ObjectId.GenerateNewId(),\r\n   FirstName = &quot;Bob&quot;,\r\n   LastName = &quot;Baker&quot;,\r\n   Age = 36\r\n}\r\n<\/pre>\n<p>Notice that we generate the Id using <em>ObjectId.GenerateNewId()<\/em>.\n<\/li>\n<li>\nOur next step is to save the new Person to the collection and this will add the data to the collection and thus the database, thus we can then query for this data afterwards using the JavaScript shell.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ncollection.Save(p);\r\n<\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This is quick post on getting up and running with MongoDB using the &#8220;Official MongoDB C# driver&#8221; and both creating a database and adding some data to it. Using Nuget, add the &#8220;Official MongoDB C# driver&#8221; from 10gen to your project Add the following using clauses using MongoDB.Bson; using MongoDB.Driver; Create a POCO object to [&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-907","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\/907","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=907"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/907\/revisions"}],"predecessor-version":[{"id":916,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/907\/revisions\/916"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}