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

Spring Data MongoDB - projection and search

I am using "Wildcard text index" in order to search for a pattern in every fields of my class. I am also using projection in order to remove a certain field: @Query(value = "{$text: { $search: ?0 }, fields = "{'notWantedField':0}") However, I would…
Mykeul
  • 498
  • 1
  • 6
  • 20
0
votes
0 answers

Spring batch MongoItemReader ignoring database index (created via Spring Data)

I'm getting the following whenever I fire off a job, database error: Runner error: Overflow sort stage buffered data usage of 33554495 bytes exceeds internal limit of 33554432 bytes I know why this is, I also know how to increase that value but…
Chris
  • 3,437
  • 6
  • 40
  • 73
0
votes
1 answer

Performance degradation in mongodb aggregation-framework

I am using mongodb 3.6.8. I have a collection (called states) with an ObjectId field (sensor_id), a date, as well as a few other fields. I created a compound index on the collection: db.states.createIndex({ "sensor_id" : 1, "date" : 1 }); I am…
0
votes
3 answers

MongoDB: How to index unknown fields

We're storing documents with a undefined structure. I mean, it has a basic structure (id, user and creationTimestamp), but is also there a Map values fields, where we are able to store whichever structure: public class Metadata { …
Jordi
  • 20,868
  • 39
  • 149
  • 333
0
votes
1 answer

PM2/NodeJS API with mongoDB index problems

I'm trying to deploy multiple NodeJS rest APIs in the same Ubuntu VPS using PM2, those APIs are used to serve data from different MongoDB database in the same local hosted db server. I do encounter some port problems, but i do change it in every…
Malek Boubakri
  • 820
  • 2
  • 17
  • 36
0
votes
1 answer

How this query should be indexed to increase performance

I have a MongoDB query Schema: Demo { a: String, b: Number, c: Bool, d: Number } Query: db.Demo.find({ a:'test', c: true }).sort({b:-1, d: -1}).limit(40).explain("executionStats") I have tried applying these index: TYPE 1…
Robins Gupta
  • 3,143
  • 3
  • 34
  • 57
0
votes
0 answers

will mongodb index work on looked up collection?

assume I have collection A like: A{ b: bid } and collection B with index on field x assume I do an aggregation on A like: { $lookup from B as b where b._id = a.bid} { $match b.x = something} will the index on B improve query?
Mars
  • 873
  • 1
  • 11
  • 24
0
votes
0 answers

Is there a way to optimize count with mongoDB

I have a index on id_profile and i do db.myCollection.count({"id_profile":xxx}). It's quite fast if the count is low, but if the count is large, it starts being slow. For example if there is 1 000 000 records matching {"id_profile":xxx} then it can…
zeus
  • 12,173
  • 9
  • 63
  • 184
0
votes
1 answer

How can i optimize my schema to remove useless default index on "_id"

This is my document structure : db.like { _id: objectid, /* not null */ id_from: int64, /* not null */ id_to: int64, /* not null */ date: datetime, /* default NOW not null */ } db.like.createIndex( {"id_to": 1, "id_from": 1}, {unique:…
vostock
  • 249
  • 1
  • 7
0
votes
0 answers

MongoDB: CreateCollection with $Lookup

I'm trying to make a MongoDB Database with a few collections. These collections have a one-to-many relationship with eachother, in the sense that: One {A} has (0..*) of {B} One {B} has (0..*) of {C} ... and a few levels deep. As per structure,…
Diamundo
  • 148
  • 11
0
votes
1 answer

MongoDB: Create unique index for nested document

Array ( [_id] => 1 [OrderId] => 123456_0203_2500001101518267176 [Items] => Array 0 => Array ( [ItemId] => "123456" [ItemDesc] => "This Item numer 123456" ), 1 => Array ( …
0
votes
1 answer

In Mongodb how can I view an index's values

I not asking how to view the indexes on a collection but how can I look inside the index and see its values? I have a field that should be unique so I created a unique index and now I want to cross verify that all the documents are present in the…
Gabriel Fair
  • 4,081
  • 5
  • 33
  • 54
0
votes
1 answer

query for indexed key values that has the most references?

We have a very big collection, with several indexes. Since an index is basically a table with the indexed field as key, and a list of ObjectIDs as value, we were wondering if we could somehow get the key that has the highest number of Objects it…
marmor
  • 27,641
  • 11
  • 107
  • 150
0
votes
0 answers

MongoDB takes different time for with and without hint

I know, I know, hint forces MongoDB to use a specific index. I have two…
Dushyant Bangal
  • 6,048
  • 8
  • 48
  • 80
0
votes
2 answers

MongoDb Indexing Performance Issue (mongodb or spring-batch)

I have 19 Million records in my mongo collection. Format of my collection is: { "_id" : ObjectId("5992d5a5e7f31a5e90abb881"), "_class" : "com.abc.Try", "field1_code" : "mycode_sdsvmnsbd7986fskljfnsv89s7fmnslfsd78", "field2_id" :…
Shashank
  • 712
  • 15
  • 33