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

expireAfterSeconds doesnt work mongo

Using Mongo 3.2 I set expireAfterSeconds to 3 days, because we do not need more then 3 days of data on this collection, but I can see in the db, that we still have data from a month ago. Is something configured wrong. The info gather from…
puppeteer701
  • 1,225
  • 3
  • 17
  • 33
2
votes
1 answer

What exactly happens when you 'index' a MongoDB database?

So I understand that indexes are crucial because they allow you to make queries without having to look at the entire collection, but what exactly is an index? What really happens to the data when you call .createIndex()? Thanks!
vince
  • 7,808
  • 3
  • 34
  • 41
2
votes
2 answers

Mongodb query - Does sequence of key matters in usage of compound index

Suppse I created index like following :- db.collection.createIndex( { propA: 1, propB: 1, propC:1 } ) and I query like following :- db.collection.find({propB:'x', propC: 'y', propA:'z'}) will mongo query engine use the index created above or…
Anand
  • 4,523
  • 10
  • 47
  • 72
2
votes
1 answer

MongoDB possibly scanning documents for an operation that could be covered by an index

I have a collection with a locked field in each document. I have the following index: { locked : 1 } when I perform this explain over a count operation db.scheduled.find({locked: false}).explain({executionStats:1}) { "queryPlanner" :…
Danilo Silva
  • 293
  • 2
  • 6
2
votes
0 answers

Improve distinct query performance using indexes

Documents in the collection follow the schema as given below - { "Year": String, "Month": String } I want to accomplish the following tasks - I want to run distinct queries like db.col.distinct('Month', {Year: '2016'}) I have a compound…
hyades
  • 3,110
  • 1
  • 17
  • 36
2
votes
1 answer

Mongo Index for which documents have a nested key?

I have objects that look like: { ... sources: { source_1: { [metadata about source_1] }, source_z: { [metadata about source_z] }, source_a: { [metadata about source_a] } } } If a document has data from a source, the entry with…
Jordan Warbelow-Feldstein
  • 10,510
  • 12
  • 48
  • 79
2
votes
0 answers

MongoDb - Indexes optimization

Indexes optimization: We reviewed all our indexes on the expensive collection [170M documents] . We started to removed most on the indexes ; There are 2 main indexes remained [not counting the primary Key]. This trimmed the total index size to 1/3…
2
votes
1 answer

About mongodb index

i have one question about mongodb index. Suppose we have two models: class Book include Mongoid::Document field :user_id field :borrower_id belongs_to :user end class User include Mongoid::Document has_many :books end Question If i…
Yijun
  • 433
  • 1
  • 3
  • 9
2
votes
1 answer

Unique and Sparse Schema-level Index MongoDB and Mongoose

I am trying to create an index on two fields of a schema that are to be unique and sparse in MongoDB using Mongoose as follows: var ArraySchema = new Schema ({ user_id: {type: mongoose.Schema.Types.ObjectId, ref:'User'}, event_id: {type:…
c1moore
  • 1,827
  • 17
  • 27
2
votes
1 answer

Index for query with "field": {$not: {$elemmatch: {...}}?

Why is this query not using this index when searching documents like these? My Query: { "unique_contact_method.enrichments": { "$not": { "$elemMatch": { "created_by.name":enrichment_name }}}} My Index: { key: {…
Jordan Warbelow-Feldstein
  • 10,510
  • 12
  • 48
  • 79
2
votes
2 answers

Which index would be used if there are multiple indexes containing the same fields?

Take, for example, a find() that involves a field a and b, in that order. For example, db.collection.find({'a':{'$lt':10},'b':{'$lt':5}}) I have two keys in my array of indexes for the collection: [ { "v" : 1, "key" : { "a" : 1, …
u3l
  • 3,342
  • 4
  • 34
  • 51
2
votes
2 answers

Mongodb using wrong index

I have multiple indices on a collection as following. Particularly I want a query to use "gTs_1_RE_H_1_l_1", but the query is using "gTs_1" instead! { "0" : { "v" : 1, "key" : { "_id" : 1 }, "ns" :…
VaidAbhishek
  • 5,895
  • 7
  • 43
  • 59
2
votes
1 answer

MongoDB Indexing 2 fields and let index be used for search on 3th field

From MongoDB Documentation If you have a compound index on multiple fields, you can use it to query on the beginning subset of fields. So if you have an index on a,b,c you can use it query on [a] [a,b] [a,b,c] So lets say i have document…
Novkovski Stevo Bato
  • 1,013
  • 1
  • 23
  • 56
1
vote
1 answer

is it advised to created two separate indexes for the query to filter on one field and sort on another?

I have a query that's something like the following find( { a : { $gt: 3 } } ).sort( { b : -1 } ) Is it advised to create an index for field a and b separately?
tom
  • 14,273
  • 19
  • 65
  • 124
1
vote
2 answers

mongodb:how to add one field to the _id index composed of a Compound index

I can't remove the _id index, why? When I try running the dropIndexes command, it removes all indexes but not the _id index. Doing 'db.runCommand' doesn't work either: > db.runCommand({dropIndexes:'fs_files',index:{_id:1}}) { "nIndexesWas" : 2,…
springchun
  • 193
  • 3
  • 13