Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we create an Index on MongoDB?

I want to create an Index on Mongo database for performance perspective, so could you please help me how I can do it?

Your help will be appreciated here.

like image 411
Chandrashekhar Patidar Avatar asked May 11 '18 22:05

Chandrashekhar Patidar


People also ask

How do I create an index in MongoDB schema?

Mongoose supports 2 syntaxes for declaring an index on a user's name. const userSchema = new Schema({ name: { type: String, index: true } // Build an index on `name` }); // Equivalent: const userSchema = new Schema({ name: String }); userSchema. index({ name: 1 }); In Mongoose, you declare indexes in your schemas.

Can we do indexing on MongoDB?

MongoDB also supports user-defined indexes on multiple fields, i.e. compound indexes. The order of fields listed in a compound index has significance.

What is the use of create index in MongoDB?

CreateIndex() Method. In MongoDB, indexes are special data structures that store some information related to the documents such that it becomes easy for MongoDB to find the right data file. The indexes are ordered by the value of the field specified in the index.


2 Answers

If you want to index on field email on users collection:

db.users.createIndex({"email":1}, {background:true})

Before applying indexing in mongodb collections you need to understand the following aspects of indexing:

Indexing strategy:

  • Check your application for what type of queries does it send to mongodb.
  • List down all such possible queries.
  • Based on the number of operations, type of operations define index type
  • Choose the correct type of indexes for application needs. Type can be single index, compound index, partial index, TTL index and so on
  • Do your queries involve the sort operations? Follow this guide on indexing for operations with sort.
  • The more detailed guide on indexing strategy here.

Test your indexes:

  • Once you have the list of indexes to be applied, test your indexes performance using explain.
  • Generate a sample application calls on your database and enable profiler (in dev or stag) to check how your indexes are performing.

How to index:

  • Create indexes in the background. It will make sure that the create index operation does not block the other operations.
  • Depending on your data size, if the indexes to be created on large collections, consider doing it in low traffic hours. Or in a scheduled maintenance window
  • You may need to consider building rolling index in certain use cases to minimize the impact of indexing.

Keep track of indexes you create:

  • Document your indexes. This may include when you have created those indexes, why and so on.

Measure your index usage stats in production:

  • Once you have applied these indexes in production, in a week or two check usage stas of your indexes to check whether they're really being used
  • Consider dropping the indexes if they're not used at all.

Caution:

  • Indexes add performance penalty for write operations. Design and apply indexes which are must for your application.
like image 89
Atish Avatar answered Sep 19 '22 12:09

Atish


The basic syntax is:

db.collection.createIndex(keys, options)

So, for example:

$ db.users.createIndex({"username" : 1})

See MongoDB Indexes for the full details.

like image 31
craigcaulfield Avatar answered Sep 19 '22 12:09

craigcaulfield