2

I need to know abt how indexing in mongo improve query performance. And currently my db is not indexed. How can i index an existing DB.? Also is i need to create a new field only for indexing.?.

1 Answer 1

2

Fundamentally, indexes in MongoDB are similar to indexes in other database systems. MongoDB supports indexes on any field or sub-field contained in documents within a MongoDB collection.

Indexes are covered in detail here and I highly recommend reading this documentation.

There are sections on indexing operations, strategies and creation options as well as a detailed explanations on the various indexes such as compound indexes (i.e. an index on multiple fields).

One thing to note is that by default, creating an index is a blocking operation. Creating an index is as simple as:

db.collection.ensureIndex( { zip: 1})

Something like this will be returned, indicating the index was correctly inserted:

Inserted 1 record(s) in 7ms

Building an index on a large collection of data, the operation can take a long time to complete. To resolve this issue, the background option can allow you to continue to use your mongod instance during the index build.

Limitations on indexing in MongoDB is covered here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank u.. I need further information about how can i add a new field to every document in a collection which i can use it for only indexing..?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.