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

Can you put a MongoDB multikey index on an subdocument array?

I currently have a assignments collection with the following structure: { _id:Object("basdfasdfasdfasdf"), name:"Entry one", ...//other properties services:[ { ...//other properties workers: [ { …
Jose
  • 10,891
  • 19
  • 67
  • 89
0
votes
2 answers

Error E11000 in creating MongoDB unique index

I have a MongoDB(v 5.0.6) collection with the following data: [{ "_id": { "$oid": "63bc06e6aa310000d6004a68" }, "key": "title", "label": "title", "type": "text", "searchable": 0 },{ "_id": { "$oid": "63bc06e6aa310000d6004a69" …
sariDon
  • 7,782
  • 2
  • 16
  • 27
0
votes
1 answer

does $lookup use indexes in the foreignField key?

In the example below, if the collection inventory has an index on the sku field, will it be used in this $lookup operation? db.orders.insertMany( [ { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 }, { "_id" : 2, "item" :…
0
votes
1 answer

How to sort an array field after modifying an element, i.e. calling $sort outside of a $push?

$sort can only be used after $pushing a new element to an array field. the Goal is to sort after modifying an element of an array during an update call. How can this be done?
0
votes
1 answer

Are there any penalties to replace the entire document when only a specific field is changed

In MongoDB, are there penalties to replace the whole document when only the value of a specific field is changed? Will it take longer to update the indexes, etc.?
0
votes
1 answer

Is there a way to specify a bin width for multi column sorting in MongoDB?

This is my sort object: { income: 1, age: 1 } The problem is that income in theory is a real number, and hence unique. So this may render age to have no effect, unless income can be binned. How to specify this in MongoDB aggregation? And would…
0
votes
1 answer

Will mongodb use 2 indexes in this $or query?

Index 1: { A: 1 } Index 2: { B: 1 } query: db.col.aggregate([ { $match: { $or: [ A: { $eq: 100 }, B: { $eq: 100 }, ] } } ]) Will mongodb use both indexes in this query?
0
votes
1 answer

Is there a way to filter array of subdocuments in the $match stage?

db.col.aggregate([ { $match: { field_nest: { $elemMatch: { /* conditions */ } } } } ]) This is my current set up. In addition to matching the parent document, it needs to also return only the subdocument that matches the…
0
votes
1 answer

will documents be returned in the order of the compound index?

Suppose that this is my compound index in my MongoDB collection: { "age": 1, "income": 1, } Will all returned records be sorted first by age and then by income?
0
votes
0 answers

MongoDB query is slow on first view runs, but then dramatically speeds up afterwards

My MongoDB Atlas collection has 1 million documents and about 10 indexes. I have a query that has arbitrary filter conditions. I notice that sometimes whenever I create a new set of filter conditions it runs really slowly in the first 4 or 5 runs,…
0
votes
1 answer

$lt and $gt queries are significantly faster than $eq on un-indexed numeric field

const result = await sandbox .aggregate([ { $match: { B: { $eq: 500 }, // B: { $lt: 500 }, faster // B: { $gt: 500 }, faster }, }, { $limit: 1000 }, …
0
votes
1 answer

Compound index - skipping a prefix vs selecting all values of a prefix

{ field_1: "string" // can only have the value of "A" or "B", field_2: "numeric", } The above is the schema for my collection. The following compound index exists: { field_1: 1, field_2: 1 } The query in question is below: db.col.find( {…
0
votes
2 answers

read queries becoming slower the more indexes I add

It seems that the more compound index I add to my collection it gets better to some point and then beyond that the more indexes the slower it becomes. Is this possible? If so why? EDITED: I am referring to read queries. not write queries. I am aware…
0
votes
1 answer

sort order of compound indexes

db.col.createIndex( { A: 1, B: -1 } ) Will this query work: db.col.find( { A: { $gt: 5 }, B: { $gt: 10 } } ) The sort order of be in query is the opposite of the sort order of the index. Why does this make a difference, if it does?
0
votes
1 answer

Indexing a field that is an object

{ field_1: { A: true, B: true } } Instead of creating indexes on field_1.A and field_1.B, what happens if I create an index on just field_1? db.coll.createIndex( { field_1: 1} )