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
1 answer

does $elemMatch in $projection stage make use of indexes?

https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/ If there are indexes for that field, will the $elemMatch operator use it during $projection?
0
votes
1 answer

Can MongoDB use multiple indexes in one query?

I am facing the decision to either use one collection with embedding or use 2 collections. If I use one collection, then I only need to perform 1 query. If I use 2 collections, then I need to perform two queries. However, performing 2 questions…
0
votes
1 answer

Command failed with error 67 :cannot create unique index over { Key.IdentifierValue:: -1 } with shard key pattern { _id: "hashed" }'

I need to avoid duplications Transactions while inserting the documents in MongoDB database . Getting below exception .The condition is if we try to insert the Key.IdentifierValue with the same value it should throw the duplicate exception but the…
Rose
  • 89
  • 5
  • 18
0
votes
0 answers

can mongodb handle queries with hundreds of filter conditions?

Supposed that a collection has billions of documents. Can mongodb handle queries that have hundreds of filter conditions on this collection by millions of users simultaneously? Assuming that this collection is indexed properly?
0
votes
0 answers

Compound index with queries that filter for multiple values of a prefixed field

db.col_name.createIndex({"A": 1, "B", 1}) db.col_name.find({A: {$in: [1, 2, 3]} B: 50}) Since three values are selected for the prefix index, would that invalid the compound index? If not, how will it solve this problem?
0
votes
1 answer

MongoDB compound indexes on array fields

{ field_1: ["a", "b", "c"], field_2: ["x", "y", "z"] } Can I create a compound index with field_1 and field_2? If not, what is my alternative to get around this?
0
votes
0 answers

What kind of field types in MongoDB that cannot be indexed?

Are there any complex field types, such as arrays or arrays of objects, that cannot be indexed in a MongoDB collection? If so, what are all field types that cannot be indexed?
0
votes
0 answers

How to write mongodb query having $in with large array to search

I am running a query which is having more values in the $in as below. My collection is having 5 million documents. Can anyone give suggestions on how to write an efficient query? { "_id":{ "$gt":objectId1, "$lt":objectId2 }, …
0
votes
1 answer

How to index a MongoDB field that holds an array of strings?

In my collection, documents will contain a field that holds an array like so: { field_a: ["banna", "orange", "kiwi"] } How to index this collection based on this field? The queries will be something like: Find all documents where field_a is a…
0
votes
1 answer

Compound MongoDB index not used for exact same query

I have index like this: businessId_1_accountId_1_dataHash_1_deleted_1 And query like this, does not use it, but uses another one. I can't understand why. db.getCollection("transactions").find({ "businessId": ObjectId("62c56bbdba7f1d001368f217"), …
Diffusion
  • 46
  • 5
0
votes
0 answers

In elastic we have copy_to parameter for copying the fields. Is there any way to replicate the same in mongodb while indexing?

{ "global_string_mv": { "match_mapping_type": "string", "match": "global*_string_mv", "mapping": { "type": "text", "copy_to": "global_fields_string_mv", "fields": { "text_en": { …
0
votes
1 answer

How will this indexed query be executed?

Imagine that you have a collection with info about goods in an antique store. Each document has the following structure: { "id": 100, "category": "furniture", "price":1000, "quantity": 10 } You've created this…
Anas Nisar
  • 104
  • 7
0
votes
1 answer

Alternative solution to `$lookup` needed because the collection in the `from` field is sharded

Query with arbitrary number of filter conditions that come from querying the same collection I am referring to the question above. Here is an additional requirement: The score table is sharded. Hence, it can no longer be in the $lookup stage. Is…
0
votes
1 answer

How does having a long array of subdocuments in a field affect performance?

{ field_1: [ {A: 1, ...} {A: 10, ...} {A: 2, ...} ] } What if field_1 has 10s of 1000s of subdocuments in its array? What kind of query performance would be negatively affected? And why? What if the subdocuments are multi key…
0
votes
1 answer

MongoDB not using index when sorting

I'm using mongo 4.0.12 and I'm trying to tune my most executed query: db.getCollection('ServiceInvoice').find( { "Provider.ParentId": "60f9d7631b1f243eb82903ee", "Provider._id": "60f9d803fa27e34fdc4ec159", "Environment": 1, "Status":…
Sergio Jr
  • 120
  • 1
  • 8