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 requests getting very slow, even with indexes

im building a mongodb app and im just wondering why (even with indexes) my requests getting very slow if documents reach an amount > 1.000.000. my schema looks like: const reportSchema = new mongo.Schema({ organisation: { type:…
Eric
  • 115
  • 1
  • 10
0
votes
0 answers

Mongoose: Query Nested Properties of a Document With $in

So I am trying to query the following document using $in from Mongoose. var Reports = [ { "employee" : { "gender" : { "id": 0, "name" : "Male" }, "race" : { …
AllJs
  • 1,760
  • 4
  • 27
  • 48
0
votes
1 answer

How to create a partial multikey index to only index documents holding non-empty arrays in the indexed field?

In MongoDB, I have documents that look like { _id: 1, name: "foo", tags: ['someTag', 'someOtherTag'], } The large majority of my documents have no tags, so their tags field is set as tags: [], but I need to frequently access the ones that do…
axanpi
  • 731
  • 9
  • 16
0
votes
1 answer

Mongo text index doesn't finish when nothing is returned

I am not sure I have the proper vocabulary to describe the error that I am having so bear with me. Here is the general schema for the documents in my collection { _id: ObjectId(), name: String, business: String, address: { …
Danwakeem
  • 328
  • 2
  • 17
0
votes
2 answers

Understanding mongodb indexing: Indexing linked document that is frequently queried

Here is a simple example of the kind of query my app makes So you have a notepad and notes collection. The note documents are linked to the notepad document using the notepadId. So a typically query looks like: Notes.find({notepadId:…
Nearpoint
  • 7,202
  • 13
  • 46
  • 74
0
votes
2 answers

MongoDB: is it better to use int64 or ObjectID for _id?

As objectID is stored on 12 bytes and are even not well for shard key i ask myself if it's not better to use instead a totally random int64 (8 bytes) for _id ? my idea, create a totally random int64, see if it's not already present in the…
zeus
  • 12,173
  • 9
  • 63
  • 184
0
votes
1 answer

Mongodb Index & search for comma separated value

I have structure like below { "name": "", "age": 35, "hobbies": "gardening, skiing, horse riding, soccer", "Address": { "addrline 1": "", "addrline 2": "", "City": "", "Zip": "" } } Would like to search based on hobby.…
Debopam
  • 3,198
  • 6
  • 41
  • 72
0
votes
2 answers

MongoDB optimize indexes for aggregation

I have an aggregate on a collection with about 1.6M of registers. That consult is a simple example of other more complex, but illustrate the poor optimization of index used in my opinion. db.getCollection('cbAlters').runCommand("aggregate",…
0
votes
1 answer

What are the tradeoffs of creating a new compound index versus adding to an existing?

I'm querying by fields a,b, and c, and have this index: {a: 1, b: 1, c: 1} I'm adding a new query on a, sorted by d desc. Should I change the index to: {a: 1, b: 1, c: 1, d: -1} Or should I add a second index: {a: 1, d: -1}
Loren
  • 13,903
  • 8
  • 48
  • 79
0
votes
1 answer

mongodb compund index vs. index intersect

There will be about 3 billion docs in the collection across shards. Assuming I would be using queries that is not fulfilled entirely by the index(es). And I am not using the keys for sorting. Does it make sense to have indexes as follows (which…
MikeJavadi
  • 21
  • 1
0
votes
1 answer

How can i get the specific array (or key of the array)?

How can i get the specific array (or key of the array) which contain "unique6" in mongoDB. Note: value inside array is unique. { "_id" : "DETbQx7i9Sunu9w88", "someKey" : { "arr1" : ["unique1", "unique2", "unique3"], …
0
votes
0 answers

MapReduce index usage in MongoDB when using both sort and query

For optimal performance, if you provide both a sort and a query to the MapReduce, should you have: one index with fields used in the sort, then the fields used in the query one index with fields used in the query, then the fields used in the…
Danny Varod
  • 17,324
  • 5
  • 69
  • 111
0
votes
1 answer

MongoDB - What would the best Shard Key be for storing Email Pixel Tracking data

At present we send emails to subscribers on our Mailing List. This features a tracking pixel that maps to a PHP file that then does Insert data into our SQL Server database. The current average for Email Tracking Pixel Inserts is about 80,000…
mrjamesmyers
  • 454
  • 4
  • 13
0
votes
1 answer

mongodb fast tags query

I have a very large collection ( more than 800k ) and I need to implement a query for auto-complete ( based on word beginnings only ) functionality based on tags. my documents look like this: { "_id": "theid", "somefield": "some value", …
jacob
  • 1,397
  • 1
  • 26
  • 53
0
votes
1 answer

Improve geoIndex performance on mongo

I have the next collection sample: var testSchema = new Schema({ title: {type: String, required: true}, owner: {type:Schema.Types.ObjectId}, locatedAt: {type: {}, index: '2dsphere', sparse: true, "2dsphereIndexVersion": 2,…
Iván Peralta
  • 851
  • 1
  • 9
  • 25