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 shard key for emails collection

I'm using MongoDB 2.6.1 I have a collection that stores the emails, project-wise. The documents are as follows(haven't included the 'Raw Email Text' key for readability) : { "_id" : ObjectId("540d4ae7eea013be22f1f0d6"), "Project_Id"…
Kaliyug Antagonist
  • 3,512
  • 9
  • 51
  • 103
0
votes
1 answer

Pymongo collection.find_and_modify, specifying index to use? $hint?

My find_and_modify query is using the wrong index. Is there a way, using PyMongo, to hint the correct index (by name or otherwise)? Nothing seems to mention this at all in any docs...
Jordan Warbelow-Feldstein
  • 10,510
  • 12
  • 48
  • 79
0
votes
1 answer

Index prefix doesn't work if a compound index contains 2dsphere index

> db.test.ensureIndex({x: 1, location: '2dsphere'}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.test.find({x: 0}).explain() { "cursor" :…
user805627
  • 4,247
  • 6
  • 32
  • 43
0
votes
1 answer

MongoDB C# OfType() Indexing behavior

Suppose I have this query over a Collection: var dbMarkers = Features.AsQueryable() .OfType() .Where(f => f.parentFeatureSetId = parentFeatureSetId ); And the features are…
user1275011
  • 1,552
  • 1
  • 16
  • 36
0
votes
0 answers

Build index which will cover queries

The steps in our application are : Tweets are being fetched and stored in a MongoDB collection viz. search_tweets Data is read from search_tweets, several parameters are calculated and the results are stored in a different collection viz.…
Kaliyug Antagonist
  • 3,512
  • 9
  • 51
  • 103
0
votes
0 answers

Mongo crashes on duplicates when there are no any

we have replica set consisting of only mongo server 2.6 (we have upgraded a week ago) . we are running on windows server 2008 on virtual machine. Recently we have noticed rather annoying issue: we have a collection called items_full which has a…
0
votes
2 answers

MongoDB cannot upsert something without valid shard key

Sorry but I am new to MongoDB I think I understand the concept of shards, and how data can be distributed across servers via a hashed shard key - however I am then unsure on how you know which shard you are to query. For example - I have a…
0
votes
2 answers

How to use full text search for unknown number of children of a field in Mongodb?

I have a document with one field description like this: { "_id": "item0", "description": { "parlist": [ { "listitem": { "text": { "child": "page rous lady", "keyword": "officer e" } …
Prakash Thapa
  • 1,285
  • 14
  • 27
0
votes
1 answer

Does MongoDB ensureIndex perform a rebuild?

I'm reading MongoFB documentation. At this url http://docs.mongodb.org/manual/tutorial/build-indexes-on-replica-sets/ I read "Create the new index using the ensureIndex() in the mongo shell, or comparable method in your driver. This operation will…
Andrea Zonzin
  • 1,124
  • 2
  • 11
  • 26
0
votes
2 answers

Mongodb Unique index

i trying to create an unique multikey index based on two fields : My document : > db.users.find().pretty() { "_class" : "bean.User", "_id" : ObjectId("52f3945e2f9d426e9dcb1ac8"), "commandes" : [ { …
Lombric
  • 830
  • 2
  • 11
  • 23
0
votes
1 answer

MongoDB geospatial search based on many dimensions

Totally stumped on this one. Say my database has documents which contain a field called userTags, which is an object that looks like this: { name: 'obj1', userTags: { "foo" : 5, "bar" : 30, "aaa" : 15, "bbb" : 21, "ccc" : 23 …
JVG
  • 20,198
  • 47
  • 132
  • 210
0
votes
2 answers

Do I need composite indices if each attribute is indexed in mongodb collection?

Suppose I have a collection in a mongo database with the following documents { "name" : "abc", "email": "abc@xyz.com", "phone" : "+91 1234567890" } The collection has a lot of objects (a million or so), and my application, apart from…
Munim
  • 6,310
  • 2
  • 35
  • 44
0
votes
1 answer

MongoDB not using even the simplest index

Please look at the following example. It seems to me that the query should be covered by the index {a: 1}, however explain() gives me an indexOnly: false. What I am doing wrong? > db.foo.save({a: 1, b: 2}); > db.foo.save({a: 2, b: 3}); >…
Sam
  • 491
  • 1
  • 4
  • 16
0
votes
1 answer

Where does compound indexes in mongodb come into play

What are the advantages we get from compound indexes. I mean suppose we have a collection, in which I have to index over 2 fields say key1 and key2. How different is it from having a compound index {key1:1, key2:1}. Whats the problem with having 2…
Sushant Gupta
  • 8,980
  • 5
  • 43
  • 48
-1
votes
0 answers

Why is an indexed $lookup not O(log(N)) complexity when added to a query?

A query passes 100,000 documents to a $lookup stage, which joins based on an indexed field. Here is the explain: { "$lookup": { "from": "user", "as": "user", "localField": "_id", "foreignField": "_id", …
1 2 3
25
26