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
1
vote
1 answer

Partial indexing of an embedded document in an array

I have the following data in a collection named articles : { "_id": "1", "attributes": [ { "id": "providercode", "value": "code1" }, { "id": "otherAttribute", "value": "very long value than will be longer than…
Julien TASSIN
  • 5,004
  • 1
  • 25
  • 40
1
vote
1 answer

How compound indexes work in mongodb?

I am using pymongo and mongoengine, Below is the collection schema. class Person(Document, BaseMixin): school_id = StringField(required = True) first_name = StringField(default="") last_name = StringField(default="") email =…
1
vote
2 answers

Expires setting in mongoose schema is ignored if index already exists at MongoDB

I'm using the expires field in a mongoose schema in order to set expiration on a given field. For instance: var Crumbs = new Schema({ ... timestamp: { type: Date, expires: 3600, required: true }, ... }); The effect…
fgalan
  • 11,732
  • 9
  • 46
  • 89
1
vote
0 answers

When and why to autoIndex with MongoDB?

I am building a web app with MongoDB. When the app connects to my database, the autoIndex flag is set to true by default. However, according to the documentation it's not recommended using it in production. The autoIndex flag rebuilds indexes and it…
Łukasz Szkup
  • 117
  • 1
  • 9
1
vote
0 answers

How to create a 2DSphere index on a array of locations?

I Have this collection : { geopositions: [ {type:"Point", coordinates: [-10, 12]}, {type:"Point", coordinates: [-25, 85]} ] } but when i try to create the 2DSphere index on geopositions with db.myCollection.createIndex(…
zeus
  • 12,173
  • 9
  • 63
  • 184
1
vote
1 answer

MongoDB partial index not working as expected with count

On mongoDB 3.6.3 I create this collection with two million records: function randInt(n) { return parseInt(Math.random()*n); } for(var j=0; j<20; j++) { print("Building op "+j); var bulkop=db.media.initializeOrderedBulkOp() ; for (var i = 0;…
zeus
  • 12,173
  • 9
  • 63
  • 184
1
vote
1 answer

Create 2dsphere index of polygon in subfield

I'm having problems creating a 2dsphere index in MongoDB 3.6. I have a series of polygons in GeoJSON. One of the polygons is the following example (simplified to just 4 points): GeoJSON { "_id" : ObjectId("5a92b5ad370dfa460e07f2ab"), …
Jumy Elerossë
  • 189
  • 2
  • 3
  • 12
1
vote
1 answer

MongoDB Storing Large Keywords Table Query Performance

I need to store a collection containing a large number of keyword objects example: { key: "tokyo", someMeta: "meta.." } db.keyword.createIndex({ key: 1 }, { unique: 1 }) I will use it as a look-up dictionary, so it will be called extremely often.…
user1955934
  • 3,185
  • 5
  • 42
  • 68
1
vote
0 answers

Indexing dynamically named nested fields

So, for some reason the developers decided to store some information using dynamically named nested fields, and now the queries on these fields are (not surprisingly) sluggish. The documents are like so: { "_id:" : "2343244324", "field" : { …
Christian Dechery
  • 876
  • 10
  • 31
1
vote
0 answers

what is multiKeyPaths in explain, and why the lack of which causes bad index usage

I have a large collection with 4 shards. When I run a query over an indexed array field "array.number" like so: var query = { "array" : { $elemMatch: { "number" : { $gte : "10", $lt : "20" } } } }; and check explain, I'll get these winning plans…
marmor
  • 27,641
  • 11
  • 107
  • 150
1
vote
1 answer

Mongodb partial index on one of the indexed field

I want to create partial index on one of the indexed field but I am failing miserably db.Comment.createIndex( { "siteId": 1, { { "parent": 1} ,{partialFilterExpression:{parent:{$exists: true}}}}, "updatedDate": 1, "label": 1 } ); how to do…
Libertarian
  • 123
  • 1
  • 9
1
vote
0 answers

MongoDB uses wrong index, even after clearing the query plan cache

I have the following command: db.mycol.find({sourceId:ObjectId("596bac5a6f473e1a042bFFFF"),myFlag:false}).count() It was taking a lot of time. So I decided to add an index { "sourceId" : -1, "myFlag" : -1 } MongoDB still kept taking the same…
Dushyant Bangal
  • 6,048
  • 8
  • 48
  • 80
1
vote
1 answer

Cannot create index - bad index key pattern

After some activities with my database, I lost my index. I had these indexes: { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "collection.statement" }, { "v" : 1, "unique" : true, "key" : { …
Durable Metal
  • 35
  • 1
  • 6
1
vote
1 answer

Why Mongo query for null filters in FETCH after performing IXSCAN

According to Mongo Documentation, The { item : null } query matches documents that either contain the item field whose value is null or that do not contain the item field. I can't find documentation for this, but as far as I can tell, both…
Andy
  • 7,885
  • 5
  • 55
  • 61
1
vote
1 answer

Mongo doesn't optimize $or query by combining two IXSCANs

I have an orders collection with the following index, among others: {location: 1, completedDate: 1, estimatedProductionDate: 1, estimatedCompletionDate: 1} I'm performing the following query: db.orders.find({ status: {$in: [1, 2, 3]}, location:…
Andy
  • 7,885
  • 5
  • 55
  • 61