Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Learn Indexing and The ensureIndex Method in MongoDB


In this chapter, we are going to explore the ‘ensureIndex’ method to operate on the documents present inside a collection of Mongodb.

Indexing in MongoDB
We all are familiar with the index of a book which has all the chapters numbered. When we have to search for a specific chapter which is present in the middle of the book without using the index, then we will have to go page by page to find that chapter. However, on the book with index the page number is mentioned with which we can directly jump on to that chapter and hence it saves our time. In the similar way, Indexes in MongoDB save our time and make the processing of queries faster. Consider the situation without indexes in MongoDB, it will scan each and every document present in a collection to search those documents that matches the query criteria. This will be a very inefficient approach when MongoDB query has to search the required documents from a large volume of data.

Indexes in MongoDB are special data structures which are used to store a small portion of the data set in an easy-to-traverse form. They store the value of a specific field or more than one fields (i.e. set of fields), which are ordered by the value of the field as indicated in the index.

The ensureIndex () Method
In MongoDB, we use ‘ensureIndex ()’ method to create an index. The following is the basic syntax for ‘ensureIndex ()’ method in the MongoDB.


Points to remember

  • In the above syntax, the KEY is the name of the document’s field on which we want to create an index and the number 1 is to create such index in the ascending order. If we want to create an index in the descending order then we need to use -1.
  • In ‘ensureIndex ()’ method, we can pass multiple fields of a document in order to create an index on multiple fields.
  • Also, the ‘ensureIndex ()’ method accepts the following list of options which all are optional.

The following is an example on using the ‘ensureIndex ()’ statement. Let’s insert 4 documents into a collection known as ‘index_demo_db’.

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\Aparajita>cd C:\Program Files\MongoDB\Server\3.2\bin

C:\Program Files\MongoDB\Server\3.2\bin>mongo.exe
MongoDB shell version: 3.2.10
connecting to: test
> use index_demo_db
switched to db index_demo_db
> ( [ { city : "Toronto" , country : "Canada" }, { city : "Washington D.C." , country : "United States" } , {city : "New Delhi" , country : "India" }, {city : "London" , country : "United Kingdom" } ] )
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 4,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
> db.index_demo_db.ensureIndex({"city":1})
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
> db.index_demo_db.find()
{ "_id" : ObjectId("584dac0d194666db0c313fe8"), "city" : "Toronto", "country" : "Canada" }
{ "_id" : ObjectId("584dac0d194666db0c313fe9"), "city" : "Washington D.C.", "country" : "United States" }
{ "_id" : ObjectId("584dac0d194666db0c313fea"), "city" : "New Delhi", "country" : "India" }
{ "_id" : ObjectId("584dac0d194666db0c313feb"), "city" : "London", "country" : "United Kingdom" }

Explanation of the code

  • Here, we are using ‘index_demo_db’ as the collection name of MongoDB.
  • Next, we are using the ‘save ()’ method to insert four records together as the bulk write operation in the MongoDB.
  • Lastly, we are using the ‘ensureIndex ({“city”:1})’ method to create an index on the ‘city’ field of the documents in the ascending order.

In this chapter, we have learnt about the practical use of the ‘ensureIndex ()’ method with the help of an example.

The post Learn Indexing and The ensureIndex Method in MongoDB appeared first on | Blog.

This post first appeared on How And When Should You Use HBase NoSQL DB, please read the originial post: here

Share the post

Learn Indexing and The ensureIndex Method in MongoDB


Subscribe to How And When Should You Use Hbase Nosql Db

Get updates delivered right to your inbox!

Thank you for your subscription