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

In Mongodb, after create the text index, when query using text filter there is no any output showing

In Mongodb, I have created the collection as follows. db.test.insertMany([ {CustomerKey : "11026", FirstName : "Harold", LastName : "Sai", BirthDate : new Date("1951-10-1"),MaritalStatus : "S", Gender : "M", EmailAddress :…
viththi
  • 161
  • 3
  • 9
0
votes
1 answer

How to check if there are index in liquibase-mongo?

Recently on production environment mongoDb version was upgraded to 4.2 so attempto to create index by the same columns but with different name is a root cause of error from mongo db side. So because of it I need to implement liqibase mongo…
user2032856
  • 23
  • 1
  • 1
  • 9
0
votes
0 answers

Mongodb - Indexing usage and application

I have an Index created based on the frequent queries. Here is the order of Index 1- (A,B,C) . Index 2 (A,B,D) , Index 3 (A,B,E). There is already Index on (A,B) and (C), (D), (E) as well. Is there any way better way to do it ? The only performance…
Musthafa
  • 13
  • 3
0
votes
1 answer

Duplicate error even though collection is empty

I try to insert multiple documents into my MongoDB collection, but whatever I do, I get a duplicate error. I made sure that there should be no duplicates possible by dropping the whole collection. I tried it with .insertMany(), .save(), .create() -…
Maurice
  • 141
  • 1
  • 9
0
votes
0 answers

How to index two arrays in MongoDB? Error: cannot index parallel array

Whenever I want to insert a new document, I receive the following error: MongoError: cannot index parallel arrays [english] [german] What's the proper way to work with two unique arrays in MongoDB? What are alternatives to my approach? My…
0
votes
1 answer

What is the best way to query an array of subdocument in MongoDB?

let's say I have a collection like so: { "id": "2902-48239-42389-83294", "data": { "location": [ { "country": "Italy", "city": "Rome" } ], "time": [ …
0
votes
0 answers

Spring MongoDB composite indexing coverage

I have a document that has fields engine, type, manufacturerId. I created a composite unique index on the 3 fields, so I used the following to create the index. @CompoundIndex(name = "engine_type_manufacturerId", def = "{'engine' : 1, 'type' : 1,…
anz
  • 987
  • 7
  • 21
0
votes
0 answers

Can I make a MongoDB Text Index on a field in a Document that is nested in another Document? (Java)

I have a document that has a nested document set as the "name" variable: Document{{name=Document{{he=דמטריוס הכרונוגראף, en=Demetrius the Chronographer}}}} It is contained in a mock database that I am meant to implement search functionality into. I…
0
votes
0 answers

Mongo misses the index

I am trying to update round 255k documents in a proximately 200GB mongo collection. If I run explain plan on the query the winning plan hits the right index. But when I run node js script that executes the same query the index is missed. I found out…
tadejcek
  • 49
  • 1
  • 5
0
votes
1 answer

Indexing MongoDB for sort consistency

The MongoDB documentation says that MongoDB doesn't store documents in a collection in a particular order. So if you have this collection: db.restaurants.insertMany( [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, {…
Johan
  • 37,479
  • 32
  • 149
  • 237
0
votes
2 answers

MongoDB cannot set default_language for text index using DataGrip

I'm tring to create a text index for multiple fields in a mongodb collection called test but when i run this script in DataGrip db.test.createIndex( {"field1.subfield1": "text", "field1.subfield2": "text"}, {name : "my_test_index", default_language:…
0
votes
2 answers

Why do we need an additional LIMIT stage with compound index in Mongo

I am using Mongo 4.2 (stuck with this) and have a collection say "product_data" with documents with the following schema: _id:"2lgy_itmep53vy" uIdHash:"2lgys2yxouhug5xj3ms45mluxw5hsweu" userTS:1494055844000 Case 1: With this, I have the following…
Naman
  • 27,789
  • 26
  • 218
  • 353
0
votes
0 answers

MongoDB / What is the best index structure for $or search in multiple fields of a collection?

With a MongoDB collection structured like this: { title: { lang1: "Title in lang1", lang2: "Title in lang2" }, description: { lang1: "Description in lang1", lang2: "Description in lang2" }, keywords:…
Ludovic
  • 223
  • 3
  • 9
0
votes
0 answers

Why I can't have similar indices(index) in different mongodb collections?

I'm using nest js for some project I'm working on and I've decided to use Typeorm. I know it's probably better to directly use Mongoose for this but I'd like to have the option to easily migrate to Postgress or MySQL in the future. From my…
Newton Munene
  • 145
  • 3
  • 12
0
votes
2 answers

Indexing not utilized during the MongoDB aggregation query

I have stuck somewhere in MongoDB aggregate query. I tried to generate a summary report from the database which contains 110M records. during the report generation, I faced the following issues 1).Even though the collection is indexed they are not…