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

MongoDb Indexing with deference of 2 dates

In my Mongo db collection have 2 date fields TimeStamp, OpEndTime, I need to take order by OpEndTime-TimeStamp. It is working with a small amount of data. For huge data throw exception out of ram space. So need to create the index with difference…
0
votes
1 answer

MongoDB - Can I use @TextIndexed and @Indexed on a field at the same time?

Is it possible to have both index and text index on the same column in MongoDB? I want to query Question collection by country code (i.e. "US") and unwind related data in Country collection by country code as id. Example code for Spring Data MongoDB…
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
0
votes
1 answer

MongoDB Compound Index Prefix with Sort

I have trouble deciding where to best put sorting field in a mongodb compound index. My understanding is that we need to choose the highest cardinality & selectivity as the preceeding fields in a compound index db.person.createIndex({ rating: 1,…
user1955934
  • 3,185
  • 5
  • 42
  • 68
0
votes
1 answer

Have unique mongodb index on different layers of tree

I have a tree-like document model like the image below. Is it possible to create a unique index for different layers? For example, in the below example, I have index field 1, then different index fields in objects of l2 array and l3 array. I am…
Sethuraman Srinivasan
  • 1,528
  • 1
  • 20
  • 34
0
votes
1 answer

MongoDB TTL/ExpireAfterSeconds is misbehaving and not deleting all data after given time

1) We have put expireAfterSeconds=15 on column of type: date [ { "v" : 1, "key" : { "_ts" : -1 }, "name" : "AnjaliIndex", "ns" : "test.sessions", "expireAfterSeconds" : 15 } ] It…
fatherazrael
  • 5,511
  • 16
  • 71
  • 155
0
votes
1 answer

MongoDB $match $group query slow

So I have the following query for about 500k documents and it is really slow on the database side: db.collection('news').aggregate( [ { $match: { feeds: { $in: feeds }, createdAt: { $lte: lastPostDate }, …
galgo
  • 734
  • 3
  • 17
  • 45
0
votes
1 answer

How to add weights as part of create index in Azure Cosmos DB Mongo API

I have setup Cosmos DB with Mongo API, it contains fell fledged documents. Now I would like to add indexes with weights like how we do in Standalone Mongo DB. Can't this be achieved? Have tried running the below in the Cosmos shell but says its not…
0
votes
1 answer

why mongo doesn't replace OR stage by SORT_MERGE stage when the entries are already sorted

I have a collection with the following two indexes: { tags: 1, views: 1 } and { author.username'': 1, views: 1 } I am looking for a way to get the result of the following query in the most optimized way searchArray = ['exam', 'ple', 'dot',…
Equitable
  • 37
  • 5
0
votes
0 answers

Create a document local index over a array

I have the following structure in my Mongo document: { property1: 'value', property2: 'value', items: [ { name: 'name1' }, { name: 'name2' }, ] } Is possible to define a local index over the items array? In…
The Matrix
  • 1,268
  • 2
  • 14
  • 23
0
votes
1 answer

MongoDB index not used when sorting, although prefix matches

I'm trying to fetch a set of records in the most efficient way from MongoDB, but it goes wrong when I add a sorting stage to the pipeline. The server does not use my intended index. According to the documentation it should however match the…
Maarten Ureel
  • 393
  • 4
  • 18
0
votes
0 answers

Indexing of NULL in mongoDB

I have a mongodb collection like { template_id: { type: String, required: true }, owner: { user_id: { type: String, required: true } }, assignee: { user_id: { type: String, required: false } } //...other…
Sharath Chandra
  • 654
  • 8
  • 26
0
votes
0 answers

Mongoose: bad hint when indexing on an array

I have a Mongoose model like const OffersSchema = new Schema({ storeIdList : [{ type: mongoose.Schema.Types.ObjectId, ref: 'Stores', required: true }], status: { type: String, required: true, …
João Otero
  • 948
  • 1
  • 15
  • 30
0
votes
0 answers

MongoDB hint() fails - not sure if it is because index is still indexing

In SSH session 1, I have ran operation to create partial index in MongoDB as follows: db.scores.createIndex( ... { event_time: 1, "writes.k": 1 }, ... { background: true, ... …
azec-pdx
  • 4,790
  • 6
  • 56
  • 87
0
votes
1 answer

MongoDB index for uniqueness value

I need an index that will provide me uniqueness of the field among all fields. For example, I have the document: { _id: ObjectId("123"), fieldA: "a", fieldB: "b" } and I want to forbid insert the document { _id: ObjectId("456"), …
user
  • 4,410
  • 16
  • 57
  • 83
0
votes
0 answers

How to create index to support multiple queries simultaneously in mongodb?

I have 3 different fields a, b, c in my collection X, where: a -> short string/keyword, b -> url, c -> date I have 2 queries which need a, b and a, c respectively. My question is, How to index properly to optimize both queries. I have…
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57