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

How mongodb indexing reduces the the number of docs to be scanned?

I have a collection like: [{a: 'eXeD9', b: 399}, {a: 'eXe9', b: 35399} , xOBJs]. I am gonna search 24823293 in b field. So as I know I am have to traverse all docs, until there is a match with 24823293. So I am confused if I create an index for b…
Muhammad
  • 2,572
  • 4
  • 24
  • 46
0
votes
0 answers

Mongodb version 4.2 Can a partial index be used while sharding the collection

My collection has only parital index on one key . I want to shard the collection based on that partial indexed key, is it possible in mongodb 4.2, not able to figure it out from official documentation. sample document in the collection { "_id" :…
0
votes
1 answer

Retrieving a section of a compound index

I have a large collection where I would like to retrieve results ordered by two fields. However, I would like to start retrieving results from deep within the results in a performant way. For example, if the documents are: { age: 25, name: "Gabe"…
bluenike
  • 13
  • 6
0
votes
1 answer

MongoDb : How to set IndexOptions background true for compound indexes in Java

For Single Field indexes in mongodb we can set the IndexOptions as below collection.createIndex( Indexes.ascending(actualIndexFieldName), new IndexOptions().background(true)); But not sure how to set the IndexOptions for a compound…
Abhi
  • 69
  • 6
0
votes
1 answer

mongoDB: does sorting by an inverted non-prefix index subset match the entire index when querying with index prefix?

MongoDB documentation https://docs.mongodb.com/manual/tutorial/sort-results-with-indexes/#sort-on-multiple-fields: For a query to use a compound index for a sort, the specified sort direction for all keys in the cursor.sort() document must match…
crunk1
  • 2,560
  • 1
  • 26
  • 32
0
votes
0 answers

how can i make UID field as unique with respect to the date range to remove duplicate rows in MongoDB

I am new to MongoDb and I want to remove duplicate rows by making UID unique based on startDate and endDate range. For example i have the following documents represents employee records.Here the employee can exists in different date range(eg: If…
0
votes
2 answers

Covered Queries do not work with partial indices

Update: I created a ticket: https://jira.mongodb.org/browse/SERVER-48777 Please see the example below. Why does 2nd query perform document lookups? db.createCollection('inventory') db.getCollection('inventory').insertMany([ { type: 'food',…
scthi
  • 2,205
  • 1
  • 18
  • 14
0
votes
1 answer

Searching strings in large mongodb collection (540GB / 4 billion documents)

I have a very large database collection of around 540GB with 4 billion items. The item contains various metadata but one important field "message" which is free text. I would like to be able to query it for things like: the message contains…
Lemex
  • 3,772
  • 14
  • 53
  • 87
0
votes
2 answers

Aggregation pipeline "latest for all distinct id" is very slow, need to create proper indexes?

Considering the following aggregation pipeline code to return newest entry for all distinct "internal_id": db.locations.aggregate({$sort: {timestamp: -1}}, {$group: {_id: "$internal_id", doc: {$first: "$$ROOT"}}}) This call takes up to 10 seconds,…
Adam C.
  • 3
  • 3
0
votes
0 answers

Please suggest appropriate mongodb index for $or condition

Below is the query that I'm using. { $match: { $or: [ { a: 1, b: 'P', c: null }, { a: 1, b: 'R', c: 'P' }, { a: 0, b: 'P', c: null }, { a: 0, b: 'R', c: 'Q' }, ], }, } Below is the index that I'm…
Viral Raval
  • 13
  • 1
  • 5
0
votes
0 answers

Query behaviour different from explain plan in MongoDB

We have a mongo collection with about 10 million records. There are two indices: expires_date and processing_state. One of the queries accesses data between two expires_date (gte and lte) and processing_state = 'PROCESSED'. The explain plan for the…
0
votes
0 answers

MongoDB index data with different fields / number collection in db

I am developing an integration API, each integrator will have his own channel, the number of channels < 1000. Different channels will be stored different data which need to be indexed differently. Is it normal to have a collection in the database…
VoidName
  • 340
  • 2
  • 16
0
votes
0 answers

Why does changing sort order affect index used?

I have a Mongo collection with 2 fields, both are dates. I also have the following indexes: {date1: 1} {date2: 1} If I perform the following query it's fast and does an IXSCAN using the date2 index backwards as…
Rick
  • 3,240
  • 2
  • 29
  • 53
0
votes
1 answer

Is mongodb "_id" index always kept in cache?

I've a mongodb database which has multiple collections. There's one additional collection, which is more like a log of all events that happen in the system. I am wondering, when I've a large collection which is rarely used, is "_id" index of that…
macpak
  • 1,190
  • 1
  • 14
  • 28
0
votes
2 answers

Mongodb index, should partialfilters also be included in the index?

I have a query like this: collection.find({ type: "person", image: { $exists: true } }, { sort: [ ["age", -1] ], limit: 9, ) Do I need to include the fields in the index if I already have them filtered? collection.createIndex( …
Harry
  • 52,711
  • 71
  • 177
  • 261