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
2 answers

Is there a way to make mongodb to use index, which may not quite fit, but lets sort results blockwise rather than all together?

I have a collection test, and a compound index on it with two fields db.test.createIndex({ i: 1, j: 1 }) When I execute following pipeline db.test.aggregate([{ $sort: { i: 1, j: 1 } }], { allowDiskUse: false }) it works fine. But this…
1
vote
1 answer

How to create a compound index with geospatial then _id?

I have a relatively straight forward query, yet I can't seem to create the proper index to make it the most efficient read that I can (or can't seem to instruct mongo to use my index). The query is: const query = { 'location.geoJson': { …
Isaac Torres
  • 151
  • 7
1
vote
2 answers

What if some documents don't have a field that is part of an index?

A collection has an indexed involved field_A. But field_A is not required. So what happens if some documents do not have this field? Will the index still work for documents that do have this field?
1
vote
1 answer

In MongoDB how to decide for a collection which fields to be indexed for a costly query

I have a collection with 1000+ records and I need to run the query below. I have come across the issue that this query takes more than a minute even if the departmentIds array has length something like 15-20. I think if I use an index the query time…
cmgchess
  • 7,996
  • 37
  • 44
  • 62
1
vote
0 answers

What is the proper way to query MongoDB matching $in with large collection?

There is deal collection containing 50 million documents with this structure { "user": , "price": } The collection has this index { "v" : 2, "unique" : false, "key" : { "user" : 1 }, "name" :…
1
vote
1 answer

How should I structure my MongoDB compond index?

I have a mongo images metadata collection consisting of the following fields: camera_name(str), photographer_name(str), resolution(str), image_size(int in MB, rounded) and timestamp(10 digit UNIX timestamp) I want to run 2 queries only: Given…
1
vote
0 answers

MongoDB E11000 dup key on insertMany but not on insertOne on empty collection with default index

Error description I write a node.js application with TypeScript using MongoDB (npm mongodb). On writing an array of objs with await collection.insertMany(objs) to a collection the error E11000 duplicate key error collection: CollectionName index:…
pinguiny
  • 71
  • 1
  • 6
1
vote
2 answers

How to create an index on array elements that match a specific filter in MongoDB?

Say that there is a collection of objects, each one containing an array of elements, with each element containing fields attributeName and attributeValue. How would one create an index over attributeValue, but only of the values whose corresponding…
1
vote
1 answer

Multikey partial index not used with elemMatch

Consider the following document format which has an array field tasks holding embedded documents { "foo": "bar", "tasks": [ { "status": "sleep", "id": "1" }, { "status": "active", …
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
1
vote
2 answers

Mongodb creating indexes for genomic ranges

We are new to Mongodb, and would like to use it to insert genomic data (165M entries), and retrieve this data by genomic coordinates (ranges). Below is the type of data we store in a single table. Where column names are…
Benoit B.
  • 11,854
  • 8
  • 26
  • 29
1
vote
1 answer

Indexing with Mongoose

I was reading the mongoose docs about indexing and want to find out whether there is a difference between field level indexing and schema level indexing. They mention that "defining indexes at the schema level is necessary when creating compound…
Sterlin V
  • 666
  • 2
  • 7
  • 12
1
vote
1 answer

Building multiple indexes with one of them being unique in Mongo

This is in continuation to Building Multiple Indexes at Once, where I am currently making use of the following commands db.test_collection_data.createIndex({"uId" : 1, "name" : 1}, {unique : 1}) db.test_collection_data.createIndex({"uId" :…
Naman
  • 27,789
  • 26
  • 218
  • 353
1
vote
3 answers

Mongodb IndexBuildRetry keep rebooting server (noIndexBuildRetry)

I have a mongo DB that refuse to restart because of a unique index problem. Since the DB doesn't start, its stuck on this reboot loop and I can't connect to it to remove the index (or clean the buggy data). I'd happily fix the unique key/index…
felix m
  • 31
  • 4
1
vote
0 answers

MongoDB inconsistent aggregate call between queries

I have two tables. videos and youtubes. I want to do a $lookup on videos.youtube and match that to youtubes._id and then $match that data based on a youtubes field. Which is working fine, but there are some huge inconsistencies between queries that…
1
vote
1 answer

Is there a way to release RAM occupied by mongodb indexes, after dropping collection?

The problem is that we have a huge dataset consists of 50 mln records and almost all fields are indexed, that causes huge consumption of RAM, and after collection is deleted resources are not released, I know that this can be solved by restarting…