Hello developers,

Lets start step-wise. I have a database named 'school' within which there is a collection named 'students' which has 200 record of students.
Now lets see sample of records in 'students' collection. There is a 'name' field and various scores related to that student

Now lets see the current index using
db.collection.getIndexes(). By default mongodb creates index using _id.

So now without using any custom indexing lets try to fetch record of a student named 'aimee Zank'.
Two records were found of that student with id 0 and 114.

Lets see how many documents were scanned by mongodb to search this result, using db.collection.find(query).explain().
We can see that mongodb scans for 200 objects to fetch that result. the scan period might not be noticeable for small sized data but what if we have millions of records? Latency will be obviously significant. To deal with this we must use indexing.

Lets create a custom index using "name" field as shown in figure. And lets get the list of indexes . It can be observed that new index named 'name_1' is created with key "name".
Now lets try to run same query as before and see the scanning result using explain(). You can see that mongodb just scanned two objects to fetch the result.(Note: before it scanned 200 objects).
This definitely decreases the latency for query search.
To delete indexes you can use dropIndex().
Hope this was useful.
Thanks for reading :)
First of all, lets acknowledge the moment of dilemma, being a MongoDB developer, we might need to choose among third-party search tools like elasticSearch, Solr etc. Yes, they might be very handy but why not to use the indexing and searching of MongoDB itself. So lets begin with Basic indexing with mongoDB and observe its scanning properties.
Lets start step-wise. I have a database named 'school' within which there is a collection named 'students' which has 200 record of students.
Now lets see sample of records in 'students' collection. There is a 'name' field and various scores related to that student
Now lets see the current index using
db.collection.getIndexes(). By default mongodb creates index using _id.
So now without using any custom indexing lets try to fetch record of a student named 'aimee Zank'.
Two records were found of that student with id 0 and 114.
Lets see how many documents were scanned by mongodb to search this result, using db.collection.find(query).explain().
We can see that mongodb scans for 200 objects to fetch that result. the scan period might not be noticeable for small sized data but what if we have millions of records? Latency will be obviously significant. To deal with this we must use indexing.
Lets create a custom index using "name" field as shown in figure. And lets get the list of indexes . It can be observed that new index named 'name_1' is created with key "name".
Now lets try to run same query as before and see the scanning result using explain(). You can see that mongodb just scanned two objects to fetch the result.(Note: before it scanned 200 objects).
This definitely decreases the latency for query search.
To delete indexes you can use dropIndex().
Hope this was useful.
Thanks for reading :)