This is my Fourth MongoDB post, in the first post we looked at how we can install MongoDB as a Windows Service, In the second post we looked at how we could do UPSERTs with MongoDB, In the third post we looked at how to sort results in MongoDB.
This post is about indexing in MongoDB, we are going to take a look at how to create indexes and how to see if the indexes are being used by MongoDB. Every index that you create in MongoDB is a secondary index, this is because MongoDB creates the default _id index for all collections. The _id index is a unique index on the _id field, you cannot delete the index on _id. MongoDB indexes use a B-tree data structure
Besides a regular one field index, you can also create the following indexes in MongoDB
1 Indexes on Embedded Fields
2 Compound Indexes
3 Multikey Indexes
4 Unique Indexes
5 Sparse Indexes
Before you go crazy and start adding indexes on every possible field in your collection, keep in mind that just like in regular databases, the more indexes you have the slower you write operations will be. Every update and insert will be a little slower because the indexes will have to be maintained.
Some limitations: A collection can't have more than 64 indexes.Index keys can't be larger than 1024 bytes. This includes the field value or values, the field name or names, and the namespace.
Let's go take a look at some of these indexes
来自 “ ITPUB博客 ” ，链接：http://blog.itpub.net/301743/viewspace-752948/，如需转载，请注明出处，否则将追究法律责任。