We learnt the uses of having an index/key on our collection and how to create the index. Now, in this video tutorial lets learn how to get index on individual collection and how to drop / remove / delete the index we’ve created.
After creating “another” collection, mongoDB engine generates default key on its “_id” field. And “system.indexes” shows all the keys present inside the database for all the collections it has. This can get messy if we have large number of collections – which we do in even slightly bigger projects.
Lets learn to create index and to optimize the database in MongoDB.
Creating “Database”: “temp”, “Collection”: “no”, and inserting 10 Million documents inside it
1
2
3
4
5
use temp
switched to db temp
for(i=0; i< = 10000000; i++)
db.no.insert({"student_id": i, "name": "Satish"});
use temp
switched to db temp
for(i=0; i< = 10000000; i++)
db.no.insert({"student_id": i, "name": "Satish"});
Since Mongo Shell is built out of JavaScript, you can pass in any valid Javascript code to it. So we write a for loop and insert 10 Million documents inside “no” collection.
“no” collection has 10 Million record, but it won’t fetch you all records at once, as it would take a lot of time and resources of your computer! So it only fetches 20 records at a time. You can iterate through next 20 documents by using command “it“.
find() method scans through all the documents present in the collection to find multiple matches for the condition. So in above case, find() method scans through 10 Million documents, hence returns the result slowly. Where as findOne() method stops scanning the collection as soon as it finds the first matching document, so findOne() returns result faster than find() method.
We create index on “student_id”. It takes little time to create the index, as we have 10 Million documents inside “no” collection.
After creating index on “student_id”, run the same command and you’ll get the results instantly – maybe it takes 0.01 ms, but the delay can’t be noticed. Why does it return results faster after creating index on “student_id”? Watch this short video lesson to know it: index / key: MongoDB
If we have 3 fields in a document – name, age, sex We could make name or age or sex or (name, age) or (name, age, sex) as index.
Assume that we make a index out of (name, age, sex) In this case, we need to use the keys from left to right.
If we use “name” in our command, it makes use of the index. If we use (“name”, “age”) in our command, it makes use of the index. If we use (“name”, “age”, “sex”) in our command, it makes use of the index.
If we use “age” in our command, it can’t use the index for its operation. If we use (“age”, “sex”) in our command, it can’t use the index for its operation.
If we use (“name”, “sex”) in our command, it simply uses “name” field and ignores “sex” field.
For convenience, mongoDB adds “_id” field to each document inserted. “_id” is unique across the collection. And index is automatically created on “_id”.
Since index information is stored in “system.indexes” collection – it consumes disk too. So we need to make sure to add indexes to only those fields which we access frequently. Also note that, each time a document is inserted, “system.indexes” collection must be updated with the new index information, which takes time, bandwidth and disk space. So we need to be careful while creating indexes.
If we chain explain() method to our command, we can know some details about the command and also evaluate about its efficiency. The above command doesn’t have multi-key enabled and it’s a Basic Cursor.
Since “names” collection was empty, we inserted 2 documents into it. Now we applied drop() method on the collection, which drops all the document present in the collections at once. It also removes the document/index/content present inside “system.indexes” collection.
Note: If you want to remove/drop all the documents present inside the collection, make use of drop() method, as it removes all the documents at once, its more efficient than remove({}) method which removes documents one by one. Use remove() method, when you want to remove one or a set of documents from the collection.