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

how to create index mongodb properly?

let say i have this huge documents. 2 of them got this array of object; { status: "A", group: "public", "created.dt": .... } { status: "A", group: "private", "created.dt": .... } i indexed and ensure like this :…
Farid Blaster
  • 974
  • 1
  • 9
  • 23
4
votes
1 answer

How MongoDB Choose Candidate Plans

I have a slow query in my application. After created two indexes it uses them with better performance in local DB. But when I deployed on production DB it still uses the origin index. Below this what I did. Properties in collection tasks: team_id,…
Shaun Xu
  • 4,476
  • 2
  • 27
  • 41
4
votes
1 answer

MongoDB not using indexes for distinct with simple query

I've had a weired behaviour with mongodb distinct query. currently, i'm using 2.6.10 version. Ok, let's create simple collection for test with explanation. from pymongo import MongoClient import random client = MongoClient('127.0.0.1',…
libbkmz
  • 641
  • 1
  • 7
  • 17
4
votes
1 answer

MongoDB refuses to use index intersection

I use MongoDB 2.6.4. My indexes looks like this: { "v" : 1, "key" : { "isFolder" : 1 }, "name" : "isFolder_1", "ns" : "Tenant_51.files", "background" :…
Roy Reznik
  • 2,040
  • 4
  • 22
  • 28
3
votes
0 answers

C#: Create Index For An Attribute In Embedded Document

I have following document in my MongoDB database: { "_id":1, "user_name":"John Doe", "addresses":[ { "_id":null, "geolocation":null, "city":"Toronto" }, { "_id":null, …
Samsul Hoque
  • 53
  • 1
  • 7
3
votes
2 answers

MongoDB stuck creating index with "Index Build: draining writes received during build" message

I have 4 rows in the test collection: { "_id" : ObjectId("5f4ce50e19b13337216dd477"), "test" : 1 } { "_id" : ObjectId("5f4ce50e19b13337216dd478"), "test" : 2 } { "_id" : ObjectId("5f4ce50e19b13337216dd479"), "test" : 3 } { "_id" :…
Bobo
  • 595
  • 5
  • 18
3
votes
1 answer

MongoDB Map Reduce: Auto-created index name too long, possible to customize?

Debugging MongoDB mapreduce is painful, so I'm not 100% sure I understand what's going on here, but I think I get the general idea... The error message I'm getting is this: mr failed, removing collectionCannotCreateIndex: namespace name generated…
DanM
  • 7,037
  • 11
  • 51
  • 86
3
votes
3 answers

Spring Data MongoDB Slow MongoTemplate.find() Performance

I'm having performance issues when querying ~12,000 user documents, indexed by 1 column, (companyId), no other filter. The whole collection only has ~27000. It took me about 12 seconds to get the ~12000 rows of data... I tried running explain for…
3
votes
0 answers

MongoDB _id index doesn't scale linearly

I have a collection with 1.5B documents and the _id index is roughly 75Gb. I'm wondering why the _id index is so large because I have another collection in the same database with 100M documents and the _id index is only 2.8Gb. It seems odd to me…
3
votes
1 answer

Unique compound index on array fields

I am trying to create mongo document with compound index. My sample doc looks like this { fname: "fname1", lname : "lname1", task : ["t11", "t12", "t13"] } { fname: "fname2", lname : "lname2", task : ["t21", "t22",…
Ramesh Papaganti
  • 7,311
  • 3
  • 31
  • 36
3
votes
1 answer

MongoDB operations order and changing winning plan

I'm currently running in some issue with a collection of few hundreds of (heavy) documents. Some of the fields of the document are pretty heavy, and we're excluding them during the projection stage. We figure out that the sorting is actually done…
3
votes
1 answer

Mongodb geospatial indexes, 2d vs 2dsphere

According to the docs it says about 2d indexes: The 2d index supports calculations on a flat, Euclidean plane. The 2d index also supports distance-only calculations on a sphere, but for geometric calculations (e.g. $geoWithin) on a sphere, store…
doc_id
  • 1,363
  • 13
  • 41
3
votes
1 answer

compound index syntax in mongodb

a quick question regarding how to define a compound index as per the definition I can create a compound index as below and it would create a compound index for student_id and…
user641887
  • 1,506
  • 3
  • 32
  • 50
3
votes
1 answer

MongoDB Indexing: Multiple single-field vs single compound?

I have a collection of geospatial+temporal data with a few additional properties, which I'll be displaying on a map. The collection has a few million documents at this point, and will grow over time. Each document has the following…
DanM
  • 7,037
  • 11
  • 51
  • 86
3
votes
1 answer

MGO TTL indexes creation to selectively delete documents

I'm working with Golang and MongoDB. I have a collection which needs to keep a document which can be persistent or volatile. Hence, if it's set an expire date (as the example expireAt) the document is considered volatile and deleted otherwise it'll…
fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44