Indexes improve the efficiency of read operations by reducing the amount of data that query operations need to process.
Create index for a collection :
The index can be defined in schema of collection. Consider the following collection Accounts:
AccountsSchema = new Schema({
name: String,
email: String,
Contact: String,
Gender: String,
Age: Number
});
Create the index for the above collection
AccountsSchema.index({ name: 1, email: 1});
It will create index of all records for the collection Accounts that will help to improve query process when selecting using find() or findOne().
To check MongoDB applied an index filter for a query shape, Use the cursor.explain() method and check the indexFilterSet field.
0 Comment(s)