Sort Direction for index
When we created the index, we specified the sort direction for the index, in this case both ascending. Sorting on this indexes would work only is they are both indexed ascending or descending. But if you ix the direction of either fields, mongo will skip indexing all together.
Assuming there ia an index on the name, and if I wanted to find the name for a certain record and just return just the name. We know that this will use the index, in this case the “name”
If we did an explain() the query plan says “indexonly” equals false. There is the notion of a covering index. The idea there is that , If I query using the index, and all the information I want is already included in the index, there is no real need to go and fetch the actual document. We can use the index itself to return the results document. In this case I wanted to find something with the name CAT, we have an index on the field name and it already has data CAT in it. There is no need to go to disk to fetch the document itself. We can use the index alone to return the document with only the name. The reason mongo did not use the “covering index” is because when I said return the name only , it also returned the ID. If we omit the ID, now index only is true
For the cases where only you need is a few fields and the fields are covered by the index that is going to be used by the query, Mongo can make efficient use of the index and not visit the documents at all.
Dead Weight Index
MongoDB is schema less, so mongo doesn’t know what fields you might have in the future , It also doesn’t keep a central account o all the fields present in all the documents. So its quiet possible and allowed, to create an index on a field name that doesn’t exist on any field in any document.
For instance none of my animals have a field name called zzz, but Mongo will ensure that an index will created should one want it later and it keeps track of it. If you mis-spell a field name, Mongo is not going to prevent you from creating it..Its not going to notify you that your documents don’t have the correctly spelled name. So you might end up creating an index that will never be hit and you wouldn’t know it. The best way to ensure that your indexes are optimized and well used is by using explain.
Background built Index
When your database is quiet large, you might want to build an index, but you should be aware of a few things – when you use the ensureindex() command without any options, The index is going to be build in the foreground. Mongo will block all write and read operations until this index rebuilding is done. That behavior can be changed to do the indexing on the background by setting the background option set to true, Mongo will build the index in the background.While building in the background read and write operations can continue, but know this that building it in the background will take a lot more time than building in the foreground. Also the structure of the index will be much larger if you build it in the background than when you build it in the foreground. (not a good option to make for a live production database)
Index Name Restrictions
Mongo lets you build compound indexes with up to 31 fields. The name of the index is a concatenation of their names and their sorting direction. so if the compound index has a long name and lots of them and I created an index using those keys
Then the generated name will be quite long. It might hit against the 128 character limit that Mongo imposes on the indexes name. That 128 includes the name of the collection. This becomes a problem if there are many fields and their names are long. This can also occur when you index very deep properties nested inside fields and sub documents.
To resolve that, you can choose what name to give your index when you create the index. In the example below, The previous index has been dropped and recreated with a smaller name called “small”. When you look at the indexes, the index has been ceated with all the fields that it was given , but the name is much shorter and certainly adheres to the 128 character limit.
That’s all there is to indexing.
For all your application development needs, visit www.verbat.com for a fiscally conscious proposal that meets your needs ( So I can keep this blog going as well!!!)
Alternatively click through the link if you found this article interesting. (This will help the companies Search engine rankings)
Leave a Reply