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

MongoDB not using my index(arrays, boolean)

I have collection full of the following documents, for example: { "_id" : ObjectId("2819738917238dgd21873"), "mailIntid" : 10000000, …
sejteN
  • 9
  • 2
1
vote
0 answers

ReactiveMongoTemplate .save not working on adding @compondindex annotation to the collection class

I have a Java class "Class A" whose object is being saved in a mongoDB using reactiveMongoTemplate.save operation in a reactive way. public class A { // field 1 // field 2 .. // field n } This had been working perfectly fine and populated a lot…
1
vote
2 answers

MongoDB - searching in array using $elemMatch slower with index than without

I have a collection with 500k documents with the following structure: { "_id" : ObjectId("5f2d30b0c7cc16c0da84a57d"), "RecipientId" : "6a28d20f-4741-4c14-a055-2eb2593dcf13", ... "Actions" : [ { …
djed
  • 141
  • 10
1
vote
0 answers

How to find which query used an existing index in mongodb?

We have reached the limit of the number of indexes allowed for a collection. We have a bunch of indexes that are used very little. It is difficult to track in code where the index was used. So is it possible to find the query which used the index?…
suku
  • 10,507
  • 16
  • 75
  • 120
1
vote
1 answer

Mongodb C# Should indexes be registered once only in a database's lifetime?

This question might be silly, but though there is alot of documentation about creating indexes and using them in mongodb, I'm yet to find something telling when exactly indexes should be set. For example, it is well precised in the docs that bson…
Damien Doumer
  • 1,991
  • 1
  • 18
  • 33
1
vote
1 answer

Why is Mongo not recognising the non-dotted version of an index?

I have a collection in Mongo like this: { 'key': { 'a': , 'b': }, 'data': } It has an index on key: db.collection.createIndex( { 'key.a': 1, 'key.b': 1 } ) Now I would expect these two queries to both use the…
TTT
  • 6,505
  • 10
  • 56
  • 82
1
vote
1 answer

big data query mongodb, aggregation, single index or compound index

i am trying to speed up a query that im performing to a collection that contains more than 10 million of documents. An example of document looks like this { nMove: 2041242, typeMove: 'Sold', date: "2016-05-18T16:00:00Z", …
Sergio Cano
  • 690
  • 3
  • 13
  • 36
1
vote
2 answers

MongoDB - Updating TTL Index value

I'm trying to update the value of the expiry date in a document. Here's the Node.js code I use to set the index everytime I want to update the document: database.collection(collectionName).createIndex({ "expires_on": 1 }, { expireAfterSeconds: 0…
1
vote
2 answers

Using an Index with Mongo's $first Group Operator

Per Mongo's latest $group documentation, there is a special optimization for $first: Optimization to Return the First Document of Each Group If a pipeline sorts and groups by the same field and the $group stage only uses the $first accumulator…
1
vote
1 answer

Auto Generated id using mongoDB

I am developing a desktop application using windows forms c#, I've created an unique index named Id in my database. But now I want it when I send a class with field 'Id' empty so the database gives it a unique value by its own. I am sorry for not…
1
vote
1 answer

MongoDB Index build process & Why does index creation blocks all the database activities

I had to create indices for a collection. I created it but it affected all the requests on the other collections in the other database. From doc, I read that index creation blocks all read/write locks on all the databases. What is the reason behind…
Gibbs
  • 21,904
  • 13
  • 74
  • 138
1
vote
1 answer

Index vs Aggregation Pipeline for Sorting

I'm developing an application using MongoDB as its database, and for sorting data, I encountered an interesting argument from a colleague that index can be used instead of aggregation pipeline for getting sorted data. I tried this and it actually…
1
vote
0 answers

Why can't mongodb effectively sort a non-prefixed sub-index of a compound index?

Why can't mongodb effectively sort a non-prefixed sub-index of a compound index? https://docs.mongodb.com/manual/tutorial/sort-results-with-indexes/#sort-and-non-prefix-subset-of-an-index for example if you index { a: 1, b: 1, c: 1 } on a collection…
Equitable
  • 37
  • 5
1
vote
1 answer

MongoDB Compass create TTL index

I'm trying to create a TTL index using mongo db compass. My document is as follows I created the following index in mongo db compass. But the ttl index doesn't seem to take effect on the existing or the new entries in the document.
usr30911
  • 2,731
  • 7
  • 26
  • 56
1
vote
1 answer

Mongo multi-field filter query and sort - optimization

I have a records collection which has primary_id (unique), secondary_id, status fields among others. The ids are alphanumeric fields (ex. 'ABCD0000') and the status is a numeric (1 - 5). One of the queries that would be frequently used is to filter…
AAHM
  • 41
  • 7