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

Get a list of collection's indexes in Meteor

How does one get the list of indexes on a collection with Meteor? Something similar to (or perhaps based on, proxying) Mongo's db.collection.getIndexes There's not much of an indexing API in Meteor yet (there will be one eventually); but I hope…
Bogdan D
  • 5,321
  • 2
  • 31
  • 32
6
votes
1 answer

MongoDB Indexing and Projection

I have a few questions about MongoDB: (1) Does indexing help with projection? (2) I have assigned a collection a number of indexes and tried to run a find with sort, and then use explain, it shows BtreeCursor index on the sorted field. Could it be…
oikonomiyaki
  • 7,691
  • 15
  • 62
  • 101
6
votes
4 answers

MongoDB 2dsphere index fails (malformed geometry?)

I'm currently trying to build an 2dsphere index, but the creation seems to fail. The document on which the index creation fails is valid geojson (according to geojsonlint). Also as far as I can see it obeys the MongoDB "Polygon" rules. I would…
coalmee
  • 1,334
  • 2
  • 16
  • 27
6
votes
3 answers

how to define a compound and hashed mongodb index?

I know that a compound index is defined like this: db.products.ensureIndex( { "item": 1, "stock": 1 } ) and a hashed a simple index like this: db.active.ensureIndex( { item: "hashed" } ) question is how to achieve both?
ramon_salla
  • 1,587
  • 1
  • 17
  • 35
6
votes
1 answer

Slow range query on a multikey index

I have a MongoDB collection named post with 35 million objects. The collection has two secondary indexes defined as follows. > db.post.getIndexKeys() [ { "_id" : 1 }, { "namespace" : 1, "domain" : 1, …
Eser Aygün
  • 7,794
  • 1
  • 20
  • 30
6
votes
1 answer

mongo db indexes on embedded documents

I have a domain object model as below... @document Profile { **social profile list:** SocialProfile { **Interest list:** { Interest { id type …
assaf_miz84
  • 687
  • 2
  • 15
  • 33
5
votes
1 answer

Which MongoDB indexes should be created for different sorting and filtering conditions to improve performance?

I have MongoDB collection with ~100,000,000 records. On the website, users search for these records with "Refinement search" functionality, where they can filter by multiple criteria: by country, state, region; by price range; by industry; Also,…
Maxím G.
  • 860
  • 10
  • 14
5
votes
3 answers

Spring Boot MongoDB Indexed with expireAfterSeconds to Auto delete Document does not work

I have a problem with the "time to live" settings in MongoDB. I created an Indexed Annotation in my Spring-Boot 2.0.2.RELEASE project in my Entity which represents my Document in the MongoDB. I set the "expireAfterSeconds" for testing to 15 seconds…
iSmo
  • 190
  • 3
  • 11
5
votes
1 answer

Both Filtering & Sorting Which of following queries will use index?

There is a collection people with the following index: {"first_name": 1, "address.state": -1, "address.city": -1, "ssn": 1} For Both Filtering & Sorting, which of the following queries will use index ? { "first_name": { $gt: "J" } }).sort({…
LuFFy
  • 8,799
  • 10
  • 41
  • 59
5
votes
1 answer

MongoDB : text index with arrays, only first term is indexed

I have a document that has the following Schema { description : String, tags : [String] } I have indexed both fields as text, but the problem is that whenever I search for a specific string within the array, it will return the document only if…
naughty boy
  • 2,089
  • 3
  • 18
  • 28
5
votes
0 answers

MongoDB Index optimization when using text-search in the aggregation framework

We are building a simplified version of a search engine on top of MongoDB. Sample data set { "_id" : 1, "dept" : "tech", "updDate": ISODate("2014-08-27T09:45:35Z"), "description" : "lime green computer" } { "_id" : 2, "dept" : "tech", "updDate": …
5
votes
3 answers

Should indexes be created for small definite size mongo collections?

Say I have a mongo collection which has a fix number of entries, which would never exceed a count of 300-400. Example: User{ String name; String phoneNumber; String address; String dob; Integer noOfCars; } Of these fields, I would like to index…
tunetopj
  • 561
  • 2
  • 5
  • 15
5
votes
1 answer

What is difference between GeoJSON & Legacy coordinate pairs in terms of mongoDb?

I am trying to use $geoNear aggregation operator of mongoDb to calculate distances of users from current location in following way : '$geoNear': { near: currentLocation, distanceField: 'distance', spherical: true, } With currentLocation…
5
votes
1 answer

MongoDB indexes on multiple fields

I've read the chapter on indexes in the MongoDB in action book and was wondering if anyone can expand upon what it talks about regarding indexes. If I have an index that covers a,b,c,d,e and I query on a,b,c the index is used. What happens if I…
Thomas Mitchell
  • 1,071
  • 2
  • 16
  • 29
4
votes
2 answers

MongoDB uses COLLSCAN when returning just _id

I want to return all IDs from a MongoDB collection and I used the code below: db.coll.find({}, { _id: 1}) But MongoDB scans the whole collection instead of reading the information from the default index { _id: 1 }. From the log: { find:…
bejvisek
  • 61
  • 5
1 2
3
25 26