We learnt how to create unique key/index using {unique: true} option with ensureIndex() method. Now lets see how we can create unique key when there are duplicate entries/documents already present inside the collection.
Since “_id” is treated as primary key in mongoDB, we can’t insert duplicate values to it. In above case, we are trying to insert value of “_id” as 1 twice – the second time around it threw an error stating the entered value as duplicate.
Before implementing unique key on field “a” we need to first remove the duplicate entries present inside our collection orelse it’ll through errors. Here we also remove the index/key on “a”, so that we can create unique key/index on “a”.
To create unique key/index, we need to make use of ensureIndex() method – first parameter being the field name to be made as unique key along with it’s value 1 or -1. 1 signifies ascending order, -1 signifies descending order. The second parameter {unique: true}, specifies that the key/index must be unique key/index, like that of “_id”.
Now append explain() method to our command, it shows us that it returns a Btree Cursor and multi-key as true. MongoDB engine need to match every element of the array present in field “a” with the scalar value of field “b”. Hence it uses Multi-Key indexing.
It’s difficult to match every combination of the array elements present inside both “a” and “b” fields. If both keys/indexes has its value as an array, then it gets complicated. Thus, mongoDB doesn’t allow both keys to be arrays. Either one of them must be a scalar value.
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