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
107
votes
7 answers

A list of indices in MongoDB?

Is there a way to see a list of indices on a collection in mongodb in shell? i read through http://www.mongodb.org/display/DOCS/Indexes but i dont see anything
Timmy
  • 12,468
  • 20
  • 77
  • 107
88
votes
6 answers

Overflow sort stage buffered data usage exceeds internal limit

Using the code: all_reviews = db_handle.find().sort('reviewDate', pymongo.ASCENDING) print all_reviews.count() print all_reviews[0] print all_reviews[2000000] The count prints 2043484, and it prints all_reviews[0]. However when printing…
sheetal_158
  • 7,391
  • 6
  • 27
  • 44
45
votes
1 answer

In Mongo, how do I display the indexes of a collection?

I just want to display all the indexes in the shell.
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
36
votes
1 answer

Mongo indexing on object arrays vs objects

I'm implementing a contact database that handles quite a few fields. Most of them are predefined and can be considered bound, but there are a couple that aren't. We'll call one of these fields 'groups'. The way we currently implement it is (each…
Ghjnut
  • 571
  • 2
  • 5
  • 9
35
votes
2 answers

Mongodb performance difference between Hash and Ascending indices (Any reason not to use hash in a not ordered field?)

In mongodb there are multiple types of index. For this question I'm interested in the ascending (or descending) index which can be used for sorting and the hash index which according to the documentation is "primarily used with sharded clusters to…
J-Rou
  • 2,216
  • 8
  • 32
  • 39
30
votes
2 answers

Mongodb background indexes - are they still background once created?

When creating an index in mongodb, you can specify the background: true flag, which causes the index creation to be non-blocking. This is great in production since you don't want the whole database locked up while creating an index that you clearly…
Leopd
  • 41,333
  • 31
  • 129
  • 167
25
votes
2 answers

Monitoring MongoDB "background operations"?

EDIT: Basically I'm looking for some hints on how to understand what background operations are running on my MongoDB instance and possibly reduce/disable them when necessary so they don't interfere with running tests. I've tried mongostat and…
joniba
  • 3,339
  • 4
  • 35
  • 49
25
votes
1 answer

2dsphere vs 2d index: which is "better"/faster?

Ι have a db where my documents are only Points. I consider adding a geospatial index. So I can either choose from a 2dsphere and a 2d one. MongoDB.org has: 2dsphere indexes support: - Calculations on a sphere - Both GeoJSON objects and legacy…
Diolor
  • 13,181
  • 30
  • 111
  • 179
24
votes
4 answers

sparse indexes and null values in mongo

I'm not sure I understand sparse indexes correctly. I have a sparse unique index on fbId { "ns" : "mydb.users", "key" : { "fbId" : 1 }, "name" : "fbId_1", "unique" : true, "sparse" : true, "background" : false, …
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
24
votes
3 answers

MongoException: Index with name: code already exists with different options

I have a mongodb collection term with following structure { "_id" : "00002c34-a4ca-42ee-b242-e9bab8e3a01f", "terminologyClass" : "USER", "code" : "X67", "terminology" : "some term related notes", "notes" : "some notes" } and a…
faizi
  • 1,265
  • 2
  • 12
  • 28
23
votes
1 answer

MongoDB regular expression with indexed field

I was creating my first app using MongoDB. Created index for a field, and tried a find query with $regex param, launched in a shell > db.foo.find({A:{$regex:'BLABLA!25500[0-9]'}}).explain() { "cursor" : "BtreeCursor A_1 multi", …
Eramir
  • 482
  • 1
  • 5
  • 18
23
votes
2 answers

when to index on multiple keys in mongodb

say I have an Item document with :price and :qty fields. I sometimes want to find all documents matching a given :price AND :qty, and at other times it will be either :price on its own or :qty on its own. I have already indexed the :price and :qty…
Evan
  • 357
  • 2
  • 8
21
votes
4 answers

MongoDB - too much data for sort() with no index error

I am using MongoDB 1.6.3, to store a big collection (300k+ records). I added a composite index. db['collection_name'].getIndexes() [ { "name" : "_id_", "ns" : "db_name.event_logs", "key" : { "_id" : 1 …
Vlad Zloteanu
  • 8,464
  • 3
  • 41
  • 58
17
votes
2 answers

mongodb: Multikey indexing structure?

I'm finding it hard to understand how exactly indexing is done on multikeys in mongodb. This is what I read about multikeys in mongodb docs on its website: 1) "Creating an index on an array element indexes results in the database indexing each…
Ari53nN3o
  • 1,202
  • 2
  • 14
  • 21
17
votes
4 answers

Mongodb - poor performance when no results return

I have Mongodb collection with about 7 million documents that represents places. I run a query that search for places that their name start with a prefix near a specific location. We have a compound index as described bellow to speed up the…
Eliezer
  • 171
  • 7
1
2 3
25 26