Questions tagged [mongodb-indexes]

Indexes provide high performance read operations for frequently used queries. Indexes are particularly useful where the total size of the documents exceeds the amount of available RAM.

An index is a data structure that allows you to quickly locate documents based on the values stored in certain specified fields. Fundamentally, indexes in MongoDB are similar to indexes in other database systems. MongoDB supports indexes on one or more fields or sub-fields contained in documents within a MongoDB collection.

Supported index types include:

Depending on the index type, additional properties such as sparse or unique may also be supported.

Core Features

MongoDB indexes have the following core features:

  • Indexes are defined on a per-collection level.

  • Indexes can enhance query performance, often dramatically. However, each index also incurs some overhead for every write operation. Consider the queries, the frequency of these queries, the size of your working set, the insert load, and your application’s requirements as you create indexes in your MongoDB environment.

  • All MongoDB indexes use a B-tree data structure. MongoDB can use this representation of the data to optimize query responses.

  • When using indexes with $or queries, MongoDB can use a separate index for each clause in the query.

  • MongoDB 2.6 added support for intersection of multiple indexes to be used to fulfill queries. In general, each index intersection involves two indexes; however, MongoDB can employ multiple/nested index intersections to resolve a query.

  • The query optimizer empirically selects the plan for a given query shape by occasionally running candidate query plans and caching the "winning" plan with the best response time. You can override the query optimizer using a hint() or index filter (MongoDB 2.6) to force a specific index to be used, however these should be used sparingly (typically only for testing)

  • Using queries with good index coverage reduces the number of full documents that MongoDB needs to store in memory, thus maximizing database performance and throughput.

Related Resources

MongoDB Manual

Blog Posts

Tools

  • Dex - Index and query analyzer for MongoDB: compares MongoDB log files and index entries to make index recommendations.
  • Professor - A web application with corresponding command-line tool to read, summarize, and interpret MongoDB profiler output (for MongoDB 2.0 and later).
390 questions
0
votes
0 answers

create compound index only on documents that contain all fields of the compound index

unique index only if field exist This question is an extension of the above. According to doc: https://www.mongodb.com/docs/manual/core/index-sparse/ It is possible to create a sparse index, which will be created on all documents that contains at…
0
votes
1 answer

unique index only if field exist

If There is a unique index on a field A, then there cannot be 2 documents where A doesn't exist. How do I specify that the uniqueness should only be enforced on documents where A actually exists?
0
votes
0 answers

How to specify that every document in an aggregation should have a reference to a large array?

In my aggregation query there is a static $lookup stage. Static in the sense that very document will have the same result from the $lookup. Hence, MongoDB will performance one lookup and cache the results. I further know that the result from this…
0
votes
0 answers

MongoDB collection index size using new array field takes more size than expected

MongoDB collection index size using new array field takes more size than expected. This is a multikey index with an array field as the preceding index column in a collection which already has millions of documents. index =…
CSK 4ever
  • 45
  • 4
0
votes
0 answers

Does mongodb sharded collection's performance deteriorate as the number of shards grow?

At what number of shards do sharded collections begin to have a negative impact on the performances of read queries? Hundreds? Thousands? what is the rule of thumb?
0
votes
2 answers

How to convert array of subdocuments into array of primary data values returned from a $lookup?

The $lookup stage will return an array of subdocuments. But the only value that I am interested in from the result of the $lookup is the the _id value. So instead of an array of { _id: ObjectId() }, how do I flatten this array into just an array of…
0
votes
0 answers

How to dynamically reference mongoDB field names?

Schema of collection: { field_1: "number", field_2: "number", ... } I have a query that may reference field_n dynamically depending on certain conditions that are not known before hand. How am I able to write an aggregation pipeline that…
0
votes
1 answer

How to ensure that $sort push missing values to the bottom regardless of ascending or descending

If I sort based on a column that has missing values, the missing values will be on top if sort is ascending. How to ensure that missing values always get pushed to the bottom regardless of ascending or descending?
0
votes
0 answers

sorting based on a field that was not modified - will it use the index?

This is a query of my APP that returns a list of users. After the $match stage, some fields will be modified and a $lookup stage is involved to find and remove blocked users. However, the distance field is not modified. Will MongoDB be smart enough…
0
votes
0 answers

I want to used indexes on joined collection in mongodb

I have two collection named as users and user_data, users is a primary collection and the user_data is a secondary collection. I have perform an aggregate query for retrieving millions of data but the query is taking too much time for execution.…
0
votes
0 answers

Want to use indexes on secondary collection of the mongodb

I have two collections mainCol and joinedCol from which using joins i am fetching around millions of records and the db query is taking excessive time, i want to use indexes on the fields of joined collection to reduce the excution time of the…
0
votes
0 answers

how to create index on a dynamic path in mongodb

I have nested documents of the following structure in MongoDB collection: { "A":{ "*":{ "C":{ "D": "value" } } } } Now here, I want to create…
0
votes
1 answer

Why MongoDB queries does COLLSCAN instead of INDEXSCAN?

There are the indexes created on a collection named "taches" > db.taches.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.taches" }, { "v" : 2, …
0
votes
0 answers

MongoDB explain shows {$in: ["bar"]} same as {$eq:"bar"} (E in ESR) so why does Atlas Suggested Index put foo at the end of compound index (R in ESR)

We have a lot of queries that are checking that a field value is in a set, but 99.9% of the time that set is a singleton. I was under the impression that MongoDB was smart enough to recognize { $in: ["bar"]} as the same as { $eq: "bar"} and explain…
Novaterata
  • 4,356
  • 3
  • 29
  • 51
0
votes
0 answers

Is it possible to avoid indexed data scan for search index queries in MongoDB?

I want to perform full-text search on a field for documents in a collection. However, I don't want to search in every document in the collection. That is, before full-text searching, I want to filter out documents, and then perform the full-text…