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
16
votes
3 answers

Partial indexes in mongodb / mongoose

In the sparse index documentation I found note about mongodb 3.2 partial indexes Changed in version 3.2: Starting in MongoDB 3.2, MongoDB provides the option to create partial indexes. Partial indexes offer a superset of the functionality of…
styopdev
  • 2,604
  • 4
  • 34
  • 55
15
votes
3 answers

MongoDB: Disk I/O % utilization on Data Partition has gone

Last time I get alert from MongoDB Atlas: Disk I/O % utilization on Data Partition has gone above 70 on nvme2n1 But I have no any ideas how can I localize / query / index / part of code / problematic collection. In what way can I perform any…
15
votes
2 answers

Advantage of a unique index in MongoDB

I've tried to search through Mongo documentation, but can't really find any details on whether queries on unique indexes will be faster than queries on non-unique indexes (given the same data) So I understand that a unique index will have high…
Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
14
votes
4 answers

Are there any tools to estimate index size in MongoDB?

I'm looking for a tool to get a decent estimate of how large a MongoDB index will be based on a few signals like: How many documents in my collection The size of the indexed field(s) The size of the _id I'm using if not ObjectId Geo/Non-geo Has…
jpredham
  • 2,149
  • 1
  • 23
  • 37
14
votes
2 answers

mongodb not using indexes when sorting?

i have a collection with these indexes: > db.message.getIndexKeys() [ { "_id" : 1 }, { "msgid" : 1 }, { "keywords" : 1, "msgid" : 1 } ] and a query like db.message.find({'keywords': {'$all':…
Bearice
  • 195
  • 1
  • 2
  • 7
14
votes
1 answer

MongoDB Index on different types

We can have { data: "hello" }, { data: 123 } in the same collection and even create a index on it. I'm curious how does mongodb manage the index behind the scene. We can't create single B-tree on different types. Right? However, I did getIndexes to…
Alice
  • 909
  • 1
  • 11
  • 15
14
votes
1 answer

Can I modify existing index in MongoDB without dropping it?

Can I modify existing index in MongoDB without dropping it ? I don't see anything about it in documentation. I have an non-unique index on String field. Collection is ~6M documents. It's replica set. I know I can delete index and add new one. But…
expert
  • 29,290
  • 30
  • 110
  • 214
13
votes
5 answers

Mongo unique index case insensitive

@CompoundIndexes({ @CompoundIndex(name = "fertilizer_idx", unique = true, def = "{'name': 1, 'formula': 1, 'type': 1}") }) public class Fertilizer extends Element implements Serializable { //class stuff } Is it possible to…
Pedro Dusso
  • 2,100
  • 9
  • 34
  • 64
12
votes
1 answer

mongoDB vs. elasticsearch query/aggregation performance comparison

This question is about choosing the type of database to run queries on for an application. Keeping other factors aside for the moment, and given that the choice is between mongodb and elastic, the key criterion is that the query should be resolved…
11
votes
3 answers

mongodb 3.4.2 InvalidIndexSpecificationOption error: The field 'unique' is not valid for an _id index specification

The command db.testCollection.createIndex( { _id: 1 }, {name: "_id_2", unique: true, background: true} ) fails on mongo version 3.4.2, but not 3.2.11. The mongo documentation indicates the version 3.4 supports both the unique and background…
icedawn
  • 111
  • 1
  • 1
  • 7
11
votes
4 answers

Create a conditional TTL in mongo

There is a particular task I want to accomplish, but I am not finding any particular way to do that. Let's say I have an app that is used to send mails. I keep a record of these emails in a collection in mongo. And using this app I can send mail…
kadamb
  • 1,532
  • 3
  • 29
  • 55
10
votes
2 answers

Why MongoDB doesn't use Index Intersection?

I am trying to reproduce the first example of index intersection instruction (http://docs.mongodb.org/manual/core/index-intersection/) but facing a problem: mongo doesn't uses both indexes My steps: Download mongo (3.0.3) and install it Run mongod:…
Alexander
  • 560
  • 3
  • 14
9
votes
2 answers

MongoDB - Unique index vs compound index

Assume a hypothetical document with 3 fields: _id : ObjectId emailAddress : string account : string Now, given a query on emailAddress AND account, which of the following two indexes will perform better: Unique index on emailAddress alone (assume…
Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
9
votes
1 answer

Understanding an index on an array of subdocuments

I've been looking into array (multi-key) indexing on MongoDB and I have the following questions that I haven't been able to find much documentation on: Indexes on an array of subdocuments So if I have an array field that looks something like: {field…
Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
8
votes
4 answers

How can I use partialFilterExpression on a mongoose model

I have created a mongoose model that has an email field. I want it to be unique if a value is provided by a user but I want it to be empty is a user has not provided any value. I have found a good mongodb reference here:…
Navish
  • 99
  • 1
  • 4
1
2
3
25 26