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

Which query faster on MongoDB with two index

I have two index in mongodb. First index field is "string" and second index field is "date". String field is partial. Why faster than I query "string" with "date" field when just "string"? How can I create an alternative? In 200 million…
0
votes
1 answer

MongoDB TTL index never deletes records

I created an index as shown below and added a createdAt field to each new record added to the db. The records should be auto-deleted after 24 hours however I have waited days and nothing has been…
0
votes
1 answer

How to add a single index to the createdAt field in a Strapi collection (MongoDB)

I have a collection in which I need to make queries filtering by date, so I’d like to add an index to the createdAt field (this field was created automatically when I created the collection in Strapi Content-types builder). I already added some…
0
votes
1 answer

MongoDB ttl index behave unexpected

We currently have a ttl index on a timestamp in MongoDB. Before we had an ttl expireAfterSeconds : 604800 seconds -> 1 week of documents. A few weeks ago we change this to a TTL index of 31536000 seconds -> 365 days. But MongoDB still removes…
Villeviktor
  • 101
  • 1
  • 5
0
votes
1 answer

How does it work when executing the $regex query in Mongodb?

I have a question about Mongodb Index. As far as I know, when the query is executed, it is found in the data sorted by the selected Index db.HASH_TAG.find({"hashtag" : { $regex :…
user3689808
  • 67
  • 1
  • 5
0
votes
2 answers

How to keep the Lived documents in MongoDB as per the TTL index?

I know that the TTL index in MongoDB set a time to live for its documents. When they live for the specified time then those documents gets deleted. So what if I want to archive those documents without losing them. is there any way to do it? I am…
0
votes
0 answers

What is the proper index format that should I use in MongoDB for this particular scenario explained below?

I have the following query to be executed on my MongoDB collection order_error. It has over 60 million documents. The main concern is I am having a $in operator within my query. I tried several possibilities of indices but none of them gave a…
0
votes
1 answer

What happens if we query the MongoDB database in the reverse order of compound index?

I have the following index Origin.SN_1_timestamp.milliseconds_-1_error_1 And I am running the following query db.getCollection("error").aggregate([ { "$match":{ "$and":[ { "error":{ "$ne":"No Error" …
0
votes
0 answers

How to optimise the FETCH stage of a Mongo query?

I have the following query db.getCollection("order_error").find({ "$and":[ { "type":"orderResult" }, { "Origin.SN":{ "$in":[ "5701097", "5701099" ] …
0
votes
1 answer

Search with AND operator in MongoDB using $text query operator

I already have text index in MongoDB. Simple search: db.news.find({ $text: { $search: "covid" } }) I get all the articles containing covid in the text. Now I want to get all the articles that contain both covid and vaccination. This query for…
0
votes
1 answer

MongoDB indexing not working (1 million records)

Configuration of remote server: Ubuntu 20.04 on Digital ocean (1vcpu, 1GM Ram, 25GB memory) MongoDb running on Digital ocean machine Local machine: MacOS, Dockerized Nodejs express server (from here I am searching through my collections) I created 2…
spatak
  • 1,039
  • 1
  • 14
  • 25
0
votes
1 answer

If I have both simple and compound index on a field, which one gets used in queries containing that field?

I have a field "productLowerCase" in my mongo documents. I created 2 indices 1. simple {"productLowerCase" : 1} 2. compound {"productLowerCase" : 1, "timestamp.milliseconds" : -1} So If I run a query which has only productLowerCase,…
0
votes
1 answer

MongoDB deep array scan: multikey compound indexing

I have a collection of customers with their visited places, organised as follows: { "customer_id": 151, "first_name": "Nakia", "last_name": "Boyle", "visited_places": [ { "country": "Portugal", "cities": [ "South…
3bst0r
  • 35
  • 3
0
votes
0 answers

How multiple indexes works on MongoDB

we have 3 cols {'x','y','z'} and there are millions of records when i create one by one index {x:1} {y:1} {z:1} collection.find({'x':'foo','y':'bar','z':'test'}) works faster but when i do multiple indexes as…
Onur Öztürk
  • 343
  • 1
  • 3
  • 12
0
votes
1 answer

MongoDb - Memory Read Vs Disk Read after firing the Same query

I am comparing against MySQL, where we fire a query the first attempt might read from disk and the second read will be from Memory. Does this happens in MongoDB? The reason is I am having a Query with AND & OR query and I have index on all fields.…
Musthafa
  • 13
  • 3