Indexes in any Database are essential in improving the performance of their data retrieval speed, and the same is true for MongoDB. By using Indexes, performance in MongoDB queries become more efficient. In advance Indexing we have:
- Indexing Array Fields
- Indexing Sub Document Fields.
If you have a document with an array field, you can create indexing for an array field. Creating an index on an array in turn creates separate index entries for each of its fields. It will separately create an index to an array field so it will help you to find the documents with array field value as well. You can verify with explain() that the array field has been used for finding documents.
db.collection.createIndex{arrayFieldName: order}
Order: 1- ascending; 2- descending.
If you want to search a document depending on the sub document field, you can create a sub-document field indexing. With indexing, you may get faster finding documents from the collection.
db.collection.createIndex{{fieldname.subDocumentFieldName: order}}
Order: 1-ascending; 2- descending