//Index
//Indexes support the efficient resolution of queries.
//To create an index you need to use ensureIndex() method of MongoDB.
db.COLLECTION_NAME.ensureIndex({KEY:1})
db.mycol.ensureIndex({"title":1})
//Here key is the name of the field on which you want to create index and 1 is for ascending order.
//To create index in descending order you need to use -1.
db.mycol.ensureIndex({"title":1,"description":-1})
//ensureIndex() method also accepts list of options (which are optional). Following is the list −
background - Boolean - Builds the index in the background so that building an index does not block other database activities. Specify true to build in the background. The default value is false.
unique - Boolean - Creates a unique index so that the collection will not accept insertion of documents where the index key or keys match an existing value in the index. Specify true to create a unique index. The default value is false.
name - string - The name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the indexed fields and the sort order.
dropDups - Boolean - Creates a unique index on a field that may have duplicates. MongoDB indexes only the first occurrence of a key and removes all documents from the collection that contain subsequent occurrences of that key. Specify true to create unique index. The default value is false.
sparse - Boolean - If true, the index only references documents with the specified field. These indexes use less space but behave differently in some situations (particularly sorts). The default value is false.
expireAfterSeconds - integer - Specifies a value, in seconds, as a TTL to control how long MongoDB retains documents in this collection.
v - index - version The index version number. The default index version depends on the version of MongoDB running when creating the index.
weights - document - The weight is a number ranging from 1 to 99,999 and denotes the significance of the field relative to the other indexed fields in terms of the score.
default_language - string - For a text index, the language that determines the list of stop words and the rules for the stemmer and tokenizer. The default value is english.
language_override - string - For a text index, specify the name of the field in the document that contains, the language to override the default language. The default value is language.
//Covered Queries
// As per the official MongoDB documentation, a covered query is a query in which −
// - All the fields in the query are part of an index.
// - All the fields returned in the query are in the same index.
// Since all the fields present in the query are part of an index, MongoDB matches the query conditions and returns the result using the same index without actually looking inside the documents. Since indexes are present in RAM, fetching data from indexes is much faster as compared to fetching data by scanning documents.
{
"_id": ObjectId("53402597d852426020000002"),
"contact": "987654321",
"dob": "01-01-1991",
"gender": "M",
"name": "Tom Benzamin",
"user_name": "tombenzamin"
}
db.users.ensureIndex({gender:1,user_name:1})
db.users.find({gender:"M"},{user_name:1,_id:0}) //Covered Query
//Since our index does not include _id field, we have explicitly excluded it from result set