{"id":933,"date":"2013-12-19T08:40:57","date_gmt":"2013-12-19T08:40:57","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=933"},"modified":"2014-04-10T12:33:59","modified_gmt":"2014-04-10T12:33:59","slug":"indexing-your-mongodb-data","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/indexing-your-mongodb-data\/","title":{"rendered":"Indexing your MongoDB data"},"content":{"rendered":"<p>By default MongoDB creates an index on the _id (ObjectId) field, but we can easily add indexes to other fields.<\/p>\n<p><strong>Using the JavaScript shell<\/strong><\/p>\n<p>In the JavaScript shell simply call <em>ensureIndex<\/em on your collection. For example in our CDDatabase we have a cds collection, so to add an index on the Artist we could writing the following\n\n[code language=\"JavaScript\"]\ndb.cds.ensureIndex({artist:1})\n[\/code]\n\nThe 1 (after the colon) tells MongoDB that this index orders items in ascending order whilst a -1 orders in descending order.\n\nWe can also create compound indexes, so for example using our DBDatabase we might create a compound index on Artist and Title such as\n\n[code language=\"JavaScript\"]\ndb.cds.ensureIndex({artist:1, title:1})\n[\/code]\n\nTo view the indexes applied to a collection simply use the following from the JavaScript shell\n\n[code language=\"JavaScript\"]\ndb.cds.getIndexes()\n[\/code]\n\nTo view all indexes on all collections of a database use\n\n[code language=\"JavaScript\"]\ndb.system.indexes.find()\n[\/code]\n\nTo remove an index we get the index name (i.e. using the collection method getIndexes()) and then type the following (the code assume we created an index on artist in ascending order)\n\n[code language=\"JavaScript\"]\ndb.cds.dropIndex(&quot;artist_1&quot;)\n[\/code]\n\n<strong>Using the 10gen drivers in C#<\/strong><\/p>\n<p>In C# using the 10gen drivers we can create an index using the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ncollection.EnsureIndex(new IndexKeysBuilder().Ascending(&quot;artist&quot;));\r\n<\/pre>\n<p>where collection is <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMongoCollection&lt;CD&gt; collection = db.GetCollection&lt;CD&gt;(&quot;cds&quot;);\r\n<\/pre>\n<p>To remove an index we can simply use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ncollection.DropIndex(new IndexKeysBuilder().Ascending(&quot;Artist&quot;));\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>By default MongoDB creates an index on the _id (ObjectId) field, but we can easily add indexes to other fields. Using the JavaScript shell In the JavaScript shell simply call ensureIndex<\/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-933","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\/933","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=933"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/933\/revisions"}],"predecessor-version":[{"id":1696,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/933\/revisions\/1696"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}