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

Will the ESR rule still work if the $sort comes after a data transforming stage but is still sorting on an untransformed field?

db.collection.aggregate([ { $match: { country: "USA", } }, { $set: { B: "cheese" } }, { $sort: { "A": 1 } }, { $limit: 10 } ]) In this pipeline, there is a $set stage which transforms…
0
votes
0 answers

ESR rule when sorting is done on a field inside of a embedded document

{ tests: [ { grade: 90 }, { grade: 80 }, { grade: 100 }, ... ] } This is the schema of my collection. I have an index on tests.grade. In my aggregation pipeline, after $elemMatch inside of a $match, there will be [ { $match:…
0
votes
2 answers

Create an Index for a document inside array inside document in MongoDB

Let's say I have this document: { "_id": objectId(), "products": [ { "product_id": "" } ] } I would like to make that the key product_id of documents in the array products must be unique for that document in the collection, (i…
0
votes
1 answer

How does mongodb use an index to count documents?

According to docs, db.collection.countDocuments() wraps this: db.collection.aggregate([ { $match: }, { $group: { _id: null, n: { $sum: 1 } } } ]) Even if there is an index, all of the matched docs will be passed into the $group to be…
0
votes
0 answers

What not to put in the metadata field of a timeseries collection

{ timestamp: ISODate("2021-05-18T00:00:00.000Z"), metadata: { sensorId: 5578, type: 'temperature' }, temp: 12, _id: ObjectId("62f11bbf1e52f124b84479ad") } This is an example of what to put into the metadata field of a…
0
votes
0 answers

MongoDB and AWS DocumentDB index behavior difference in bulk upsert

I'm using AWS DocumentDB with MongoDB 4.0 compatibility, so I'm using MongoDB on a docker container (single instance, not a cluster) for localhost development and AWS DocumentDB (cluster) for production. In some part of application I need to do a…
0
votes
1 answer

How to query collection for documents created at a certain time range using the ObjectId field?

I have a collection that has the default _id field, which stores the ObjecId which contains the timestamp of creation. How do I write a query to find all docs created between MIN_DATE and MAX_DATE? EDITED: It must still be able to use the index.
0
votes
0 answers

will a hashed index on _id be faster than the default b tree index that MongoDB creates?

hashed indexes, if evenly distributed, has time complexity of O(1), while b tree index always has complexity of O(log(N)). So the question is, is the ObjectId that MongoDB creates for the _id field be evenly distributed such that the time complexity…
0
votes
1 answer

how to prevent retrieval of data from disk in a $lookup?

In the $lookup stage of my aggregation pipeline, it needs to join based on _id, which is indexed on the joined collection. The purpose is to simply check whether there are any matches in joined collection. The actual data of the joined document(s)…
0
votes
1 answer

speed performance: $in vs $lookup with indexed field, _id

Assumption: All data and indexes are in RAM. First query: { $in: [user_id, array_of_user_ids_with_length_1000] } Second query: { $lookup: { from: "col_b", localField: "_id", foreignField: "user_id", as: "joined_field" …
0
votes
1 answer

MongoDB - does scanning indexes require first retrieving the index from disk?

Do indexes always persist on RAM? Hence, does scanning indexes require first retrieving the index from disk? EDITED: My questions is more about whether or not MongoDB will keep the index on RAM always, assuming that there is enough space. Because…
0
votes
1 answer

Can a query still use an index to sort a field if the index was not the one chosen in the winning plan?

I have a compound index and an index on a single field A. If in a find query, the compound index was chosen as the winning plan, and the results are sorted by the field A, will field A's index be used to sort it?
0
votes
1 answer

How to generate unique id for each element of an array field in MongoDB

How to create a unique ID for each element of an array field, where uniqueness is maintained globally for all documents of the collection? Is it possible to specify create a unique index for this field?
0
votes
0 answers

Selecting a specific element when multiple matches inside of $filter and $addField

Is there a way to filter array of subdocuments in the $match stage? This question is an extension of the above. Considering the answer of the question, what if in addition, if there are multiple subdocuments that match but you only want a specific…
0
votes
2 answers

How to transform an array field into a value equaling its maximum?

{ name: "use_name", grades: [ {class: "math": grade: 100}, {class: "english": grade: 90} ] } How do I write an aggregation pipeline to output: { name: "use_name", grades: {class: "math": grade: 100}, } The grades field…