0% found this document useful (0 votes)
103 views21 pages

MongoDB Indexing Essentials

The document discusses indexes in MongoDB. It defines what indexes are and how they improve query performance. It then covers the different types of indexes including single field, compound, multikey, geospatial, text, and hashed indexes. The document demonstrates how to create indexes, view existing indexes, hint queries to use indexes, and use explain to compare query performance with and without indexes.

Uploaded by

apoorva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views21 pages

MongoDB Indexing Essentials

The document discusses indexes in MongoDB. It defines what indexes are and how they improve query performance. It then covers the different types of indexes including single field, compound, multikey, geospatial, text, and hashed indexes. The document demonstrates how to create indexes, view existing indexes, hint queries to use indexes, and use explain to compare query performance with and without indexes.

Uploaded by

apoorva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Part 4: Index in MongoDB

Before Index
 What does database normally do when we query?
 MongoDB must scan every document (known as Collection Scan).
 Inefficient because process large volume of data

db.users.find( { score: { “$lt” : 30} } )


Definition of Index
 Definition
 Indexes are mechanisms using which the records in MongoDB are
retrieved faster and more efficiently. MongoDB creates special data
structures that store a small portion of the collection’s data set in an
easy to traverse form.

Index

Diagram of a query that uses an index to select


Index in MongoDB
Operations
 Creation index
 db.users.createIndex( { score: 1 } )
 Show existing indexes
 db.users.getIndexes()
 Drop index
 db.users.dropIndex( {score: 1} )
 Explain—Explain
 db.users.find().explain()
 Returns a document that describes the process and
indexes
 Hint
 db.users.find().hint({score: 1})
 Overide MongoDB’s default index selection
Index in MongoDB
Index • Single Field Indexes • Geospatial Indexes
Types: • Compound Field Indexes • Text Indexes
• Multikey Indexes • Hashed Indexes

• Single Field Indexes


– db.users.createIndex( { score: 1 } )
Index in MongoDB
Types • Single Field Indexes • Geospatial Indexes
• Compound Field Indexes • Text Indexes
• Multikey Indexes • Hashed Indexes

• Compound Field Indexes


– db.users.createIndex( { userid:1, score: -1 } )
Index in MongoDB
Types • Single Field Indexes • Geospatial Indexes
• Compound Field Indexes • Text Indexes
• Multikey Indexes • Hashed Indexes

• Multikey Indexes
– db.collection.createIndex({ addr.zip:1} )
Index in MongoDB
Types • Single Field Indexes • Geospatial Indexes
• Compound Field Indexes • Text Indexes
• Multikey Indexes • Hashed Indexes

In MongoDB, you can store geospatial data as GeoJSON


objects or as ’legacy coordinate pairs’. Geospatial data
means location data which are represented in latitudes
and longitudes, geospatial shapes and points.

Geospatial Indexes are of two types:


• 2d Index
• 2d Sphere Index

•2d Index is used for legacy coordinate pairs (MongoDB ver


2.2 or earlier)
•2d Sphere Index supports queries that calculate geometries
on earth-like sphere.
Index in MongoDB
Types • Single Field Indexes • Geospatial Indexes
• Compound Field Indexes • Text Indexes
• Multikey Indexes • Hashed Indexes

• Text Indexes
– MongoDB provides Text Indexes to search
queries on string content. Text  indexes can
include any field whose value is a string or an
array of string elements.
– Text Indexes drop language specific stop words
and use simple language specific suffix stemming.

– db.collection.createIndex({ field: “text”} )


– Eg:
– db.library.createIndex({bookContents : “text”})
– This example creates a text index on bookContents field
of library collection.
Index in MongoDB
Types • Single Field Indexes • Geospatial Indexes
• Compound Field Indexes • Text Indexes
• Multikey Indexes • Hashed Indexes

• Hashed Indexes
– Hashed indexes maintain entries with hashes of
the values of the indexed field.
Hashed indexes support  sharding using hashed shard keys. Hashed based
sharding uses a hashed index of a field as the shard key to partition data across a
sharded cluster.

– Hashed indexes use a hashing function to compute the hash of the value of the
index field. 

– db.collection.createIndex( { _id: "hashed" } )


Demo of indexes in MongoDB
 Import Data
 Create Index
 Single Field Index
 Compound Field Indexes
 Multikey Indexes
 Geospatial Indexes
 Text Indexes
 Hashed Indexes
 Show Existing Index
 Hint
 Single Field Index
 Compound Field Indexes
 Multikey Indexes
 Explain
 Compare with data without
indexes
Demo of indexes in MongoDB

 Import Data db.zips.createIndex({pop: -1})


 Create Index db.zips.createIndex({state: 1, city: 1})
 Single Field Index
db.zips.createIndex({loc: -1})
db.zips.createIndex({city: “text”})
 Compound Field Indexes Db.zips.createIndex({_id: “hashed”})
 Multikey Indexes
 Geospatial Indexes
 Text Indexes
 Hashed Indexes
 Show Existing Index
 Hint
 Single Field Index
 Compound Field Indexes
 Multikey Indexes
 Explain
 Compare with data without
indexes
Demo of indexes in MongoDB

 Import Data
 Create Index
 Single Field Index
 Compound Field Indexes
 Multikey Indexes
 Geospatial Indexes
 Text Indexes
 Hashed Indexes

 Show Existing Index


 Hint
 Single Field Index
 Compound Field Indexes
 Multikey Indexes

 Explain
 Compare with data without
indexes
Demo of indexes in MongoDB
 Import Data
 Create Index
 Single Field Index
 Compound Field Indexes
 Multikey Indexes
 Geospatial Indexes
 Text Indexes
 Hashed Indexes
 Show Existing Index
 Hint
 Single Field Index
 Compound Field Indexes
 Multikey Indexes
 Explain
 Compare with data without
indexes
Demo of indexes in MongoDB
 Import Data
 Create Index
 Single Field Index
 Compound Field Indexes
 Multikey Indexes
 Geospatial Indexes
 Text Indexes
 Hashed Indexes

 Show Existing Index


 Hint
 Single Field Index
 Compound Field Indexes
 Multikey Indexes

 Explain
 Compare with data
without indexes
Demo of indexes in MongoDB

 Import Data
 Create Index
 Single Field Index
 Compound Field Indexes
 Multikey Indexes

 Show Existing Index


 Hint
 Single Field Index
 Compound Field Indexes
 Multikey Indexes

 Explain
 Compare with data without
indexes
Demo of indexes in MongoDB

 Import Data
 Create Index
 Single Field Index
 Compound Field Indexes
 Multikey Indexes

 Show Existing Index


 Hint
 Single Field Index
 Compound Field Indexes
 Multikey Indexes

 Explain
 Compare with data without
indexes
Demo of indexes in MongoDB
 Import Data
Without
 Create Index Index
 Single Field Index
 Compound Field Indexes
 Multikey Indexes

 Show Existing Index


 Hint
 Single Field Index
 Compound Field Indexes
 Multikey Indexes

 Explain
With Index
 Compare with data
without indexes
Properties of MongoDB Indexes
Indexes in MongoDB can have the following properties:
 TTL Indexes: The TTL index is used for TTL collections, which expire data after a
period of time.
 Unique Indexes: A unique index causes MongoDB to reject all documents that
contain a duplicate value for the indexed field.
 Partial Indexes: A partial index indexes only documents that meet specified filter
criteria.
 Case Insensitive Indexes: A case insensitive index disregards the case of the index
key values.
 Hidden Indexes: A hidden index is not visible to the query planner.
 Sparse Indexes: A sparse index does not index documents that do not have the
indexed field.
Limitations of MongoDB Indexes
Indexes in MongoDB can have the following limitations:
 A single collection in MongoDB can have a maximum of 64 indexes.
 A compound index can have a maximum of 32 fields per index.
 An index on _id (objectId) cannot be hidden.
 A query cannot have both text and graphical indexes.
For eg: A $text query cannot be combined with $near operator.
Ordering in MongoDB

MongoDB provides sort() method for ordering records in a collection.

 cursor.sort({fieldName : <sort order>})


 Sort order is 1 for Ascending order, and -1 for descending order.
 Examples:
 db.library.find().sort({authorName : 1})
 db.library.find().sort({ISBN : -1})
 db.library.find().sort({authorName: 1, ISBN: -1})
where db.library.find() returns a cursor.

Note: Consistent or Stable results are not guaranteed by sort() method if the
method is invoked on duplicate data.

You might also like