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

Mongodb compound index with sort on _id unique index

I have a collection with document like: { _id: "", reportId: "a", accountId: "", .... } Now my query pattern is like: db.saleReport.find({reportId: "e6044e8a", "accountId":{$in: ["a","b"] }).sort({"_id":1}); I have created a compound…
sabu
  • 1,969
  • 4
  • 18
  • 28
2
votes
1 answer

MongoDB - Weird difference in _id Index size

I have two sharded collections on 12 shards, with the same number of documents. The shard key of Collection1 is compound (two fields are used), and its document consists of 4 fields. The shard key of Collection2 two is single, and its documents…
Nicholas Kou
  • 173
  • 2
  • 13
2
votes
3 answers

What is the correct way to Index in MongoDB when big combination of fields exist

Considering I have search pannel that inculude multiple options like in the picture below: I'm working with mongo and create compound index on 3-4 properties with specific order. But when i run a different combinations of searches i see every time…
VitalyT
  • 1,671
  • 3
  • 21
  • 49
2
votes
4 answers

$geoNear requires a 2d or 2dsphere index, but none were found

Add schema.index({startlocation: '2dsphere'}) in schema but not able to clear the error. schema tourSchema.index({ startLocation: '2dsphere' }); --> this line is add in model controller exports.getDistances = catchAsync(async (req, res, next) =>…
singu
  • 21
  • 1
  • 4
2
votes
1 answer

Mongoose discriminator and indexes

If I setup a Model with a discriminator key mongoose automatically prefixes all queries with the discriminator key. But if I define some keys as index: true the indexes that are created are not prefixed by the discriminator key. Instead they are…
Snowball
  • 1,402
  • 2
  • 17
  • 31
2
votes
0 answers

MongoDB: using long text as _id

background & needs I have documents in MongoDB with a natural primary key of type text (an url or a sentence, let's call it text). I need to ensure uniqueness. I also often use partial searches (text contains substring), but this part is less…
Derlin
  • 9,572
  • 2
  • 32
  • 53
2
votes
1 answer

How to create "in" operator query for multiple fields?

I have a collection db.list.insert({ first: "apple", second: "banana" }); db.list.insert({ first: "apple", second: "tree" }); db.list.insert({ first: "orange", second: "tree" }); db.list.insert({ first: "medal", second: "sport" }); I want to query…
2
votes
1 answer

Can IndexOptionsConflict be avoided to change expiredAfterSeconds in MongoDB index?

I can create an index with expiredAfterSecconds this way in the mongo shell: db.x.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } ) I can re-do the same command and that's ok for mongo: > db.x.createIndex( { "createdAt": 1 }, {…
fgalan
  • 11,732
  • 9
  • 46
  • 89
2
votes
0 answers

MongoError: user not allowed to create 2d index

I am creating a program to list nearby objects based on the user's current location. For this I am using the old [longitude, latitude] system. I will store these coordinates in the location field. The following error shows up when I try to create a…
2
votes
2 answers

MongoDB query (range filter + sort) performance tuning

I have a Mongo collection containing millions of documents with the following format: { "_id" : ObjectId("5ac37fa989e00723fc4c7746"), "group-number" : NumberLong(128125089), "date" : ISODate("2018-04-03T13:20:41.193Z") } And I want to…
Kuikiker
  • 156
  • 13
2
votes
0 answers

MongoDB index on SECONDARY node in replicaset cluster - how to delete?

Scenario: Have a MongoDB replicaset cluster with 3 nodes. Just before issuing index build command state was as following: NODE 1: SECONDARY NODE 2: SECONDARY NODE 3: PRIMARY At moment 1, issued command on NODE 3 (PRIMARY) to create…
azec-pdx
  • 4,790
  • 6
  • 56
  • 87
2
votes
2 answers

Why is mongodb not using full index

I have a collection with one 4 key compound index: > db.event.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", }, { "v" : 2, "key" : { "epochWID" :…
na_ka_na
  • 1,558
  • 1
  • 12
  • 15
2
votes
1 answer

Mongo date range index with filters

We have the below query db.Comment.find( { $and: [ { reportCount: { $gt: 0 } }, { assignee: { $exists: false } }, { creationDate: { $gt: new Date(1507831097809) } }, { creationDate: {…
user1324887
  • 632
  • 3
  • 11
  • 32
2
votes
0 answers

Index on nested document in MongoDB

I have a nested JSON document like: { "docId": 1901603742, "sl": [ {"slid","val"}], "accounts": { "123": { "smartAccountId": "123", "smartAccountName": "Dummy name", …
Prashant Mishra
  • 330
  • 4
  • 15
2
votes
1 answer

MongoDB multikey index performance

Background I have a collection of users with structure of documents like this: { "_id" : ObjectId("54e61137cca5d2ff0a8b4567"), "login" : "test1", "emails" : [ { "email" : "test1@example.com", "is_primary"…