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
votes
0 answers

How to set unique index on a field from an array of subdocuments

Schema: { A: [ { _id: "string" }, { _id: "string" }, { _id: "string" }, { _id: "string" }, ] } Requirement, the A._id field must be unique for the entire collection, not just unique across the document that it lives. { "key":…
-1
votes
1 answer

$limit in a scatter and gather query to a sharded collection

user.aggregate([ { $match: { age: { $gt: 18 }, city: { $in: ["chicago", "paris"] } } }, { $sort: { last_logged_in: -1 } }, { $limit: 10000 } ]) If the user collection is partitioned into 10 shards,…
-1
votes
1 answer

Is there a significant performance difference between sorting by just one field vs sorting by multiple fields?

Assuming that there is no index, is there a significant performance difference between sorting based on just one field vs multiple fields? db.movies.aggregate([ { $sort: { likes: -1 } } { $limit: 100 } ]) vs db.movies.aggregate([ { $sort: {…
-1
votes
1 answer

Ensuring that the $sort stage will only hold the number of documents specified in the $limit stage

Consider the case where $limit comes immediately after $sort: db.col.aggregate([ { $match: { } }, { $sort: { } }, { $limit: 10 } ]) I would assume that in the query above, the $sort stage would hold at most 10 documents, and hence RAM is no…
-1
votes
1 answer

how to $sort by field of subdocument belonging to an array?

Schema: { A: [ { name: "string", age: "integer" }, { name: "string", age: "integer" }, { name: "string", age: "integer" }, ... ] } compound index: { "A.name": 1, "A.age": 1, } query db.col.aggregate([ { $match: { A: {…
-1
votes
1 answer

100mb aggregation limit - does object fields contribute their reference size or actual value size to this limit?

I am referring to an uncorrelated $lookup. Each document passing through this stage will receive the same array computed in the $lookup. What if the actual size of this array exceeds 100mb? Does it matter? An array is a reference type, so does the…
-1
votes
1 answer

What is the size in bytes of the string return from ObjectId.toString()

const myObjectId = ObjectId("507c7f79bcf86cd7994f6c0e") const myObjectIdString = myObjectId.toString() myObjectId has a size of 12 bytes. But what about the size of myObjectIdString?
-1
votes
2 answers

Can a unique field be a prefix in a compound index?

Does it make any sense at all, since the each value of the prefix will have only one value of any suffix, there is no further sorting that can be done after the prefix?
-1
votes
1 answer

Marshall a Map into BSON by always retaining the same order

I have a map, which I create from a slice of strings. I then want to marshal this into bson format, to insert into mongodb as an index. However, because of how maps are created in Golang, I get a different ordering of the index each time (sometimes…
testing495
  • 212
  • 2
  • 12
-1
votes
2 answers

How does MongoDB compound indexes actually work?

I was creating compound indexes in mongodb, I found a weird behaviour. I created an Index: db.getCollection('Subject').createIndex({a:1, b:2, c:3}) it created an index named a_1_b_2_c_3. Now when i am using the mongo find…
Bishal Jaiswal
  • 1,684
  • 13
  • 15
-2
votes
0 answers

Will mongodb use 2 indexes on an `$or` query?

index: { A: 1, B: 2 } { A: 1, C: 2 } Query: user.aggregate([{ $match: { A: "house", $or: [ { B: "car" }, { C: "boat" }, ] } } ]) In order to check the $or condition, will mongodb…
-2
votes
1 answer

The purpose of sharding in mongoDB

I have a collection that only holds 10 million documents, totaling 10 gigabytes. This may not seem like enough to necessitate sharding. But there is a query that takes 1000 seconds to complete on this collection. If I divide this collection into…
-2
votes
1 answer

Is it efficient to store sha256 hash of username in _id field in MongoDB?

I have a special need to hash the username, and also I need to keep the usernames unique for my purpose. So, my question is can I override default '_id' field of mongoDB with sha256 hash of username? I read that _id needs to be unique and already…
Prithvi Reddy
  • 11
  • 1
  • 5
-2
votes
1 answer

Implementing an index that would improve a query in mongoDB

I'm currently struggling to implement an index to my query. Here is the original query: *db.getCollection('products').aggregate([ { $unwind: "$categories" }, { $unwind: "$categories"}, {$group: {"_id": "$_id","title":…
-3
votes
1 answer

$lookup is super slow despite the from collection being empty

The base collection has 100,000 documents. All of which will be run through a $lookup stage. The collection that will be looked up in is empty. This query takes 30 seconds. When I remove the $lookup stage, this query takes 3 seconds. What explains…
1 2 3
25
26