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

MongoDB add fields of low cardinality to compound indexes?

I have read putting indexes on low cardinality fields is pointless. Would this hold true for a compound index as such: db.perms.createIndex({"owner": 1, "object_type": 1, "target": 1}); With queries as such: db.perms.find({"owner": "me",…
Realistic
  • 1,038
  • 1
  • 10
  • 20
3
votes
1 answer

MongoDB. db.collection.explain().find() vs db.collection.find().explain()

What is the difference between this two commands? db.collection.explain().find() db.collection.find().explain()
Sergey P.
  • 194
  • 1
  • 12
3
votes
1 answer

Create an index in mongodb with more than one field using java

I am new to MongoDB. By default, collections in mongodb have index on _id field.I need to create an index on 2 more fields using Java. DBObject indexOptions = new BasicDBObject(); indexOptions.put(field_1, 1); indexOptions.put(field_2,…
Anand
  • 20,708
  • 48
  • 131
  • 198
3
votes
1 answer

MongoDB indexing for different datatypes of same key

Suppose I have a three documents where "B" field is present. All the three have 3 different datatypes. How the Index BTREE is stored? And making "B" as an index key is efficient? Example. {_id:"1",a:1,b:"abc" } //B is string here…
manikawnth
  • 2,739
  • 1
  • 25
  • 39
3
votes
1 answer

MongoDB index effectiveness evaluation best practices

We would like to evaluate the effectiveness of our indexes in a MongoDB-based REST service setup. The idea is to populate a collection with a synthetic dataset (e.g. 10,000,000 documents) then run a load injector process doing random REST operations…
fgalan
  • 11,732
  • 9
  • 46
  • 89
3
votes
2 answers

Why MongoDB cannot use a compound index that is much similar(not exact) to the query?

Consider the below Mongo index strategy and the query, Index: db.collec.ensureIndex({a:1,b:1,c:1}); Query: db.collec.find({"a":"valueA"},{"_id":0,"a":1,"c":1}).sort({"c":-1}).limit(150) The explain on the above query returns: /* 0 */ { …
vivek_jonam
  • 3,237
  • 8
  • 32
  • 44
3
votes
4 answers

What is the _id_hashed index for in mongoDB?

I sharded my mongoDB cluster by hashed _id. I checked the index size, there lies an _id_hashed index which is taking much space: "indexSizes" : { "_id_" : 14060169088, "_id_hashed" : 9549780576 }, mongoDB manual says…
zach
  • 184
  • 2
  • 8
3
votes
1 answer

MongoDB: why doesn't sorting by multiple keys use an index?

Question: I have a very large collection that is indexed by field ts: (timestamp) > db.events.ensureIndex({'ts': -1}) I want to get last 5 entries. What surprises me is that the query doesn't use the index and is thus very slow: >…
johndodo
  • 17,247
  • 15
  • 96
  • 113
3
votes
2 answers

Mongo Triple Compound Index

If you have a double compound index { a : 1, b : 1}, it makes sense to me that the index won't be used if you query on b alone (i.e. you cannot "skip" a in your query). The index will however be used if you query on a alone. However, given a triple…
Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
2
votes
2 answers

Build index against counter fields

For a field that is functioning as counter, ie, value will be changed over the time, and will be used to return ordered entities (will sort against this field for the filtered entities), should we build index for this field?
tom
  • 14,273
  • 19
  • 65
  • 124
2
votes
0 answers

I want to use indexes on joined collection in mongodb

I have two collection named as users and user_data, users is a primary collection and the user_data is a secondary collection. I have perform an aggregate query for retrieving millions of data but the query is taking too much time for execution.…
2
votes
1 answer

Embedding vs linking

In my Users collection, each document represents a user of my app. Each user may create up to N sets of filters. Each set of filter forms a document of up to 10 kb. Suppose that N = 100, would embedding be preferred? .... At N = what would linking…
2
votes
0 answers

Using MongoDB Multikey indexes for covered queries

I have a dynamic schema on MongoDB for different workflow templates. Each workflow template has data attributes associated which is stored as normal [{ _id : "1", team_id : "ABC", template_id : "retail_sales", document_id :…
2
votes
0 answers

At what point does mongoose create indexes in mongoDB?

I am trying to understand when indexes are made in mongodb. I have just learnt that The unique Option is Not a Validator. But I have been using it as a validator in my mongoose schemas for a while now and it has been working. The problem that I have…
YulePale
  • 6,688
  • 16
  • 46
  • 95
2
votes
2 answers

create index on existing collection on a MongoDB replica set (PRIMARY)

I want to add an index on an existing collection in a MongoDB replica set. This replica set has only a Primary, at the moment, and is configured in this way to be able to use MongoDB change streams. I log into Mongo shell using: mongo -u…
EanX
  • 475
  • 4
  • 21