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
1
vote
2 answers

In Mongo, are unique indexes supported for sharded environments?

We're exploring Mongo for a new website and we want to ensure no two users sign up with the same email address, so, a unique index on the email field is necessary. Is this supported in a sharded Mongo environment? Mongo documentation states unique…
user646584
  • 3,621
  • 5
  • 25
  • 27
1
vote
1 answer

Multikey indexing, performance drawbacks

Consider a collection of approximately 1 mln. documents having the following schema: { _id: ObjectId(), Title: "", Description: "", Scheduling: { From: 20230202, To: 20230201, MagicValue: 12 }, …
1
vote
2 answers

MongoDB createIndexes() Method - Support for individual Index Options

Does MongoDB createIndexes() method, support specifying individual options for each index set in the script. Something like this below, which is not working by the way. Ref -…
CSK 4ever
  • 45
  • 4
1
vote
2 answers

MongoDB ESR Index Rule - Range Filter and Sorting

The following statement regarding range filter and sorting and the way the predicates had to be placed is confusing. "MongoDB cannot do an index sort on the results of a range filter. Place the range filter after the sort predicate so MongoDB can…
CSK 4ever
  • 45
  • 4
1
vote
1 answer

Is there any way to align the order of data written to Mongo with the index order created by Cygnus?

I am investigating Cygnus for FIWARE Orion historical data persistence. Since Cygnus 3.0.0, indexes are created according to the data model when writing to MongoDB, but the order of the indexes created and the data written to MongoDB are…
1
vote
0 answers

deleteMany increases index size

I have a collection with 100,000 documents, with index totalling 80MB. Then I dropped all documents by running: col.deleteMany({}) After the operation, the index size increased to 190.46MB. What explains this? EDITED: And now it has settled at…
1
vote
1 answer

At how many elements will $in be a slow operation?

In my app, users can block other users. There will be queries where I will need to find $in: [use_id_x, array_that_contains_all_blocked_user_ids] At what length of array_that_contains_all_blocked_user_ids will this operation become slow. If it is…
1
vote
0 answers

Will $lookup still be slow if it is joining by _id?

Suppose that after $match stage, 10,000 documents will be passed into a $lookup stage. For each of those 10,000 documents, the $lookup will need to join based on its _id. The foreign field to join by will be indexed and almost unique - as it is made…
1
vote
1 answer

expression filters and object filters yield different results when null / undefined is involved

When used in the $match stage, these two aggregation stages return different documents: $expr: { $eq: ["$cheese", undefined] } { cheese: {$eq: undefined} } The field cheese is not a property of any document in the collection. In the…
1
vote
1 answer

What is the time complexity to find the value of a property of a field that is an object

Instead of: { A: [user_id_1, user_id_2, etc.] } I want to create this schema: { A: { user_id_1: true, user_id_2: true, etc... } } The reason is because in order to find if user_id_x is $in A, if it is an array, the time…
1
vote
1 answer

dynamic field name in `pipeline` of lookup where the value of the dynamic field name is determined by a variable set in `let`

user schema: { _id: "OjectId", } interaction schema: { blocker_id: user_id_x blocked: { user_id_y: true, user_id_z: true, etc... } } db.user.aggregate([{ $lookup: { …
1
vote
0 answers

Significant query performance difference when using $or

Compound Index: { "A": 1, "C": 1, "D": 1, "E": 1, "F": 1, "G": 1, "H": 1, "I": 1, "B": 1 } Fast query: const result = await sandbox .aggregate([ { $match: { A: { $in: [1, 2] }, …
1
vote
1 answer

does $unwind preserve indexes if array field has multi key index?

{ A: [ { B: "number", C: "number" } ] } Supposed that there is a multi key index on A. Does the query below preserve that? db.col.aggregate( [ { $unwind: "$A" }, { $match: { } } ] )
1
vote
1 answer

Compound indexes with continuous numeric fields as the prefix

Can MongoDB create compound indexes using continuous numeric fields as the prefix? For example: db.col.createIndex({height_to_weight_ratio: 1, income_to_expense_ratio: 1}) As I understand it, a compound index works with categorical data because for…
1
vote
0 answers

turning an array field into an embedded document field for compound indexing

According to this issue: MongoDB compound indexes on array fields compound index with more than one array field is not allowed. So let's suppose that the array field is contained. The elements can only come from the enum: ["A", "B", "C"]. And the…