SlideShare ist ein Scribd-Unternehmen logo
1 von 45
MongoDB and Indexes
Doug Duncan
dugdun@hotmail.com
@dugdun
What we’ll cover
• What are indexes?
• Types
• Properties
• Why use indexes?
• How to create indexes.
• Commands to check indexes and plans.
What are indexes?
What are indexes?
Indexes are special data-structures that store a subset of
your data in an easily traversable format.
MongoDB stores indexes in a b-tree format which allows for
efficient access to the index content.
Proper index use is good and makes a system run
optimally. Improper index use can bring a system to a
grinding halt.
What are indexes?
Indexes are stored similar in a format similar to the
following if there was an index on Origin:
[ABE] -> 0xa193b48c
[ABE] -> 0x8e8b242a
[ABE] -> 0x0928cdc1
…
[DEN] -> 0x24aa4ecd
[DEN] -> 0x87396a3c
[DEN] -> 0x9392ab2f
…
[LAX] -> 0x89ccede0
…
Types of indexes
• _id
• Simple
• Compound
• Multikey
• Full-Text
• Geo-spatial
• Hashed
The _id index
• The _id index is automatically created and cannot be removed.
• This is the same as a primary key in traditional RDBMS.
• Default value is a 12-byte ObjectId:
• 4-byte time stamp
• 3-byte machine id
• 2-byte process id
• 3-byte counter
Simple index
• A simple index is an index on a single key
• This is similar to a book’s index where you look
up a word to find the pages it’s referenced on.
Compound index
• A compound index is created over two or more
fields in a document
• This is similar to a phone book where you can
find the phone number of a person given their
first and last names.
Multikey index
• A multikey index is an index that’s created on a
field that contains an array.
• If using in a compound index, only a single field
in a given document can be an array.
• You will get one entry in the index for every item
in the array for the given document. This means
if you have an array with 100 items, that
document will have 100 index entries.
Full-text index
• This is an index over a text based field, similar to
how Google indexes web pages.
Geo-spatial index
• A geo-spatial index will allow you to determine
distance from a given point.
• Works on both planar and spherical geometries.
Hashed indexes
• A hashed index is used in hash based sharding,
and allows for a more randomized distribution.
• Hashed indexes cannot contain compound keys
or be unique.
• Hashed indexes can contain the key in both a
hashed and non-hashed version. The non-
hashed version will allow for range based
queries.
Index properties
• Unique
• Sparse
• TTL
• Partial (new in 3.2)
Unique
• The unique property allows for only a single
value for the indexed field, or combination of
fields for a compound index
db.collection.createIndex({“email”: 1}, {“unique”:
true})
• A unique index can only have a single null or
missing field value for all documents in the
collection.
Sparse
• The sparse property allows you to index only
documents that contain a value for the given
field.
db.collection.createIndex({“kids”: 1}, {“sparse”: true})
• A sparse index will not be used if it would result in
an incomplete result set, unless specifically
hinted.
db.collection.find({“kids”: {“$gte”: 5})
TTL
• The TTL property allows for the automatic removal of
documents after a given time period.
db.collection.createIndex({“accessTime”: 1}, {“expireAfterSeconds”:
“1200”})
• The indexed field should contain an ISODate() value. If
any other type is used the document will not be removed.
• The TTL removal process runs once every 60 seconds so
you might see the document even though the time has
expired.
Partial
• The partial property allows you to index a subset
of your data.
db.collection.createIndex({“movie”: 1, “reviews”: 1},
{“rating”: {“$gte”: 4}})
• The index will not be used if it would provide an
incomplete result set (similar to the sparse
index).
Why use indexes?
Why use indexes?
• Efficiently retrieving document matches
• Equality matching
• Inequality or range matching
• Sorting
• Lack of a usable index will cause MongoDB to
scan the entire collection.
How to create indexes.
Before creating indexes
• Think about the queries you will be running and try to
create as few indexes as possible to support those
queries. Similar query patterns could use the same
(or very similar) indexes.
• Think about the data that you will query and put your
highly selective fields first in the index if possible.
• Check your current indexes before creating new
ones. MongoDB will allow you to create indexes with
the same fields in different orders.
Simple indexes
• When creating a simple index, the sort order,
ascending (1) or descending (-1), of the values
doesn’t matter as much as MongoDB can walk
the index forwards and backwards.
• Simple index creation:
db.flights.createIndex({“Origin”: 1})
Compound indexes
• When creating a compound index, the sort order, ascending (1) or
descending (-1), of the values starts to matter, especially if the index is used
to sort on multiple keys.
• When creating compound indexes you want to add keys to the index in the
following key order:
• Equality matches
• Sort fields
• Inequality matches
• A compound index will also help any queries that are made based off the
left most subset of keys.
Compound indexes
• Compound index creation:
db.flights.createIndex({“Origin”: 1, “Dest”: 1, “FlightDate”: -1})
• Queries supported:
db.flights.find({“Origin”: “DEN”})
db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”})
db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”}).sort({“FlightDate”: -1})
db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”}).sort({“FlightDate”: 1})
Compound indexes
• An index created as follows:
db.flights.createIndex({“Origin”: 1, “Dest”: -1})
Could be used with either of the following queries as well
since MongoDB can walk the index either way:
db.flights.find().sort({“Origin”: 1, “Dest”: -1})
db.flights.find().sort({“Origin”: -1, “Dest”: 1})
Full-text indexes
• Full-text index creation:
• db.messages.createIndex({“body”: “text”})
• To search using the index finding any of the words:
db.messages.find({“$text”: {“$search”: “some text”}})
• To search using the index finding a phrase
db.message.find({“$text”: {“$search”: “”some text””}}
Covering indexes
• Covering indexes are indexes that will answer a
query without going back to the data. For example:
db.flights.createIndex({“Origin”: 1, “Dest”: 1, “ArrDelay”:
1, “UniqueCarrier”: 1})
• The following query would be covered as all fields
are in the index:
db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”},
{“UniqueCarrier”: 1, “ArrDelay”: 1, “_id”:
0}).sort({“ArrDelay”: -1})
Indexing nested
fields/documents
• Let’s say you have documents with nested documents in them like the
following:
db.locations.findOne()
{
“_id”: ObjectId(…),
…,
“location”: {
“state”: “Colorado”,
“city”: “Lyons”
}
}
Indexing nested
fields/documents
• You can index on embedded fields by using dot
notation:
db.locations.createIndex({“location.state”: 1})
Indexing nested
fields/documents
• You can also index embedded documents
db.locations.createIndex({“location”: 1})
• If you do this the query must match the document exactly
(keys in the same order). That means that this will return the
document:
db.locations.find({“location”: {“state”: “Colorado”, “city”:
“Lyons”})
• But this won’t:
db.locations.find({“location”: {“city”: “Lyons”, “state”:
“Colorado”})
Index Intersection
• Index intersection is when MongoDB uses two or more
indexes to satisfy a query.
• Given the following two indexes:
db.orders.createIndex({“qty”: 1})
db.orders.createIndex({“item”: 1})
• Index intersection means a query such as the following
could use both indexes in parallel with the results being
merged together to satisfy the query:
db.orders.find({“item”: “ABC123”, “qty”: {“$gte”: 15}})
Indexing arrays
• You can index fields that contain arrays as well.
• Compound indexes however can only have a single field that is an array in a given document. If
a document has two indexed fields that are arrays, you will get an error.
db.arrtest.createIndex({“a”: 1, “b”: 1})
db.arrtest.insert({"b": [1,2,3], "a": [1,2,3]})
cannot index parallel arrays [b] [a]
WriteResult({
"nInserted": 0,
"writeError": {
"code": 10088,
"errmsg": "cannot index parallel arrays [b] [a]"
}
})
Index Intersection
• Index intersection is when MongoDB uses two or more
indexes to satisfy a query.
• Given the following two indexes:
db.orders.createIndex({“qty”: 1})
db.orders.createIndex({“item”: 1})
• Index intersection means a query such as the following
could in theory use both indexes in parallel with the results
being merged together to satisfy the query:
db.orders.find({“item”: “ABC123”, “qty”: {“$gte”: 15}})
Removing indexes
• The command to remove indexes is similar to the
one to create the index.
db.flights.dropIndex({“Origin”: 1, “Dest”: -1})
Commands to check
indexes and index usage
View all indexes in a
database
• To view all indexes in a database use the
following command:
db.system.indexes.find()
• For each index you’ll see the fields the index was
created with, the name of the index and the
namespace (db.collection) that the index was
built on.
View indexes for a given
collection
• To view all indexes for a given collection use the
following command:
db.collection.getIndexes()
• This returns the same information as the
previous command, but is limited to the given
collection.
View index sizes
• To view the size of all indexes in a collection:
db.collection.stats()
• You will see the size of all indexes and the size
of each individual index in the results. The sizes
are in bytes.
How to see if an index is
used
• If you want to see if an index is used, append the
.explain() operator to your query
db.flights.find({“Origin”: “DEN”}).explain()
• The explain operator has three levels of verbosity:
• queryPlanner - this is the default, and it returns the winning query plan
• executionStats - adds execution stats for the plan
• allPlansExecution - adds stats for the other candidate plans
Notes on indexes.
• When creating an index you need to know your
data and the queries that will run against it.
• Don’t build indexes in isolation!
• While indexes can improve performance, be
careful to not over index as every index gets
updated every time you write to the collection.
Q & A
End Notes
• User group discounts
• Manning publications: www.manning.com
• Code ‘ug367’ to save 36% off order
• APress publications: www.appress.com
• Code ‘UserGroup’ to save 10% off order
• O’Reilly publication: www.oreilly.com
• Still waiting to get information
End Notes
• Communication
• Twitter: @MUGDenver and #MUGDenver
• Email: mugdenver@gmail.com
• Slack: ???
End Notes
• MongoDB World
• When: June 28th and 29th
• Where: NYC
• Save 25% by using code ‘DDuncan’

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexingMahabubur Rahaman
 
Introduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerIntroduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerMydbops
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBMongoDB
 
MongodB Internals
MongodB InternalsMongodB Internals
MongodB InternalsNorberto Leite
 
Flexible Indexing with Postgres
Flexible Indexing with PostgresFlexible Indexing with Postgres
Flexible Indexing with PostgresEDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBRavi Teja
 
MongoDB Shell Tips & Tricks
MongoDB Shell Tips & TricksMongoDB Shell Tips & Tricks
MongoDB Shell Tips & TricksMongoDB
 
MongoDB Performance Tuning
MongoDB Performance TuningMongoDB Performance Tuning
MongoDB Performance TuningPuneet Behl
 
NoSQL and MapReduce
NoSQL and MapReduceNoSQL and MapReduce
NoSQL and MapReduceJ Singh
 
MongoDB Aggregation Performance
MongoDB Aggregation PerformanceMongoDB Aggregation Performance
MongoDB Aggregation PerformanceMongoDB
 
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...MongoDB
 
cassandra
cassandracassandra
cassandraAkash R
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)Ehtisham Ali
 
Introduction to elasticsearch
Introduction to elasticsearchIntroduction to elasticsearch
Introduction to elasticsearchhypto
 
Mastering the MongoDB Shell
Mastering the MongoDB ShellMastering the MongoDB Shell
Mastering the MongoDB ShellMongoDB
 

Was ist angesagt? (20)

Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexing
 
Index in sql server
Index in sql serverIndex in sql server
Index in sql server
 
Introduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerIntroduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizer
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
MongodB Internals
MongodB InternalsMongodB Internals
MongodB Internals
 
Mongo DB Presentation
Mongo DB PresentationMongo DB Presentation
Mongo DB Presentation
 
Indexing
IndexingIndexing
Indexing
 
Flexible Indexing with Postgres
Flexible Indexing with PostgresFlexible Indexing with Postgres
Flexible Indexing with Postgres
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
MongoDB Shell Tips & Tricks
MongoDB Shell Tips & TricksMongoDB Shell Tips & Tricks
MongoDB Shell Tips & Tricks
 
MongoDB Performance Tuning
MongoDB Performance TuningMongoDB Performance Tuning
MongoDB Performance Tuning
 
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
Views, Triggers, Functions, Stored Procedures,  Indexing and JoinsViews, Triggers, Functions, Stored Procedures,  Indexing and Joins
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
 
NoSQL and MapReduce
NoSQL and MapReduceNoSQL and MapReduce
NoSQL and MapReduce
 
MongoDB Aggregation Performance
MongoDB Aggregation PerformanceMongoDB Aggregation Performance
MongoDB Aggregation Performance
 
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
 
cassandra
cassandracassandra
cassandra
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)
 
Introduction to elasticsearch
Introduction to elasticsearchIntroduction to elasticsearch
Introduction to elasticsearch
 
MongoDB 101
MongoDB 101MongoDB 101
MongoDB 101
 
Mastering the MongoDB Shell
Mastering the MongoDB ShellMastering the MongoDB Shell
Mastering the MongoDB Shell
 

Andere mochten auch

Performance Tuning on the Fly at CMP.LY
Performance Tuning on the Fly at CMP.LYPerformance Tuning on the Fly at CMP.LY
Performance Tuning on the Fly at CMP.LYMongoDB
 
Webinar: Index Tuning and Evaluation
Webinar: Index Tuning and EvaluationWebinar: Index Tuning and Evaluation
Webinar: Index Tuning and EvaluationMongoDB
 
Indexing with MongoDB
Indexing with MongoDBIndexing with MongoDB
Indexing with MongoDBlehresman
 
Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)MongoSF
 
MongoDB Performance Tuning
MongoDB Performance TuningMongoDB Performance Tuning
MongoDB Performance TuningMongoDB
 
MongoDB Days UK: Indexing and Performance Tuning
MongoDB Days UK: Indexing and Performance TuningMongoDB Days UK: Indexing and Performance Tuning
MongoDB Days UK: Indexing and Performance TuningMongoDB
 
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based ShardingWebinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based ShardingMongoDB
 
Everything You Need to Know About Sharding
Everything You Need to Know About ShardingEverything You Need to Know About Sharding
Everything You Need to Know About ShardingMongoDB
 
Sharding Methods for MongoDB
Sharding Methods for MongoDBSharding Methods for MongoDB
Sharding Methods for MongoDBMongoDB
 
Optimizing MongoDB: Lessons Learned at Localytics
Optimizing MongoDB: Lessons Learned at LocalyticsOptimizing MongoDB: Lessons Learned at Localytics
Optimizing MongoDB: Lessons Learned at Localyticsandrew311
 

Andere mochten auch (10)

Performance Tuning on the Fly at CMP.LY
Performance Tuning on the Fly at CMP.LYPerformance Tuning on the Fly at CMP.LY
Performance Tuning on the Fly at CMP.LY
 
Webinar: Index Tuning and Evaluation
Webinar: Index Tuning and EvaluationWebinar: Index Tuning and Evaluation
Webinar: Index Tuning and Evaluation
 
Indexing with MongoDB
Indexing with MongoDBIndexing with MongoDB
Indexing with MongoDB
 
Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)
 
MongoDB Performance Tuning
MongoDB Performance TuningMongoDB Performance Tuning
MongoDB Performance Tuning
 
MongoDB Days UK: Indexing and Performance Tuning
MongoDB Days UK: Indexing and Performance TuningMongoDB Days UK: Indexing and Performance Tuning
MongoDB Days UK: Indexing and Performance Tuning
 
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based ShardingWebinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
 
Everything You Need to Know About Sharding
Everything You Need to Know About ShardingEverything You Need to Know About Sharding
Everything You Need to Know About Sharding
 
Sharding Methods for MongoDB
Sharding Methods for MongoDBSharding Methods for MongoDB
Sharding Methods for MongoDB
 
Optimizing MongoDB: Lessons Learned at Localytics
Optimizing MongoDB: Lessons Learned at LocalyticsOptimizing MongoDB: Lessons Learned at Localytics
Optimizing MongoDB: Lessons Learned at Localytics
 

Ähnlich wie MongoDB Indexes Guide

Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleMongoDB
 
unit 4,Indexes in database.docx
unit 4,Indexes in database.docxunit 4,Indexes in database.docx
unit 4,Indexes in database.docxRaviRajput416403
 
Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)MongoDB
 
Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27MongoDB
 
Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26kreuter
 
Mongo db a deep dive of mongodb indexes
Mongo db  a deep dive of mongodb indexesMongo db  a deep dive of mongodb indexes
Mongo db a deep dive of mongodb indexesRajesh Kumar
 
Indexing and Query Optimizer
Indexing and Query OptimizerIndexing and Query Optimizer
Indexing and Query OptimizerMongoDB
 
About elasticsearch
About elasticsearchAbout elasticsearch
About elasticsearchMinsoo Jun
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data ModelingDATAVERSITY
 
Mongo indexes
Mongo indexesMongo indexes
Mongo indexesparadokslabs
 
Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)MongoDB
 
Test driving Azure Search and DocumentDB
Test driving Azure Search and DocumentDBTest driving Azure Search and DocumentDB
Test driving Azure Search and DocumentDBAndrew Siemer
 
An Introduction to Elastic Search.
An Introduction to Elastic Search.An Introduction to Elastic Search.
An Introduction to Elastic Search.Jurriaan Persyn
 
Automated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index RobotAutomated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index RobotMongoDB
 
Indexing In MongoDB
Indexing In MongoDBIndexing In MongoDB
Indexing In MongoDBKishor Parkhe
 
Indexing and Query Optimisation
Indexing and Query OptimisationIndexing and Query Optimisation
Indexing and Query OptimisationMongoDB
 
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2Antonios Giannopoulos
 
2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongo2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongoMichael Bright
 

Ähnlich wie MongoDB Indexes Guide (20)

Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You Scale
 
Nosql part 2
Nosql part 2Nosql part 2
Nosql part 2
 
unit 4,Indexes in database.docx
unit 4,Indexes in database.docxunit 4,Indexes in database.docx
unit 4,Indexes in database.docx
 
Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)
 
Query Optimization in MongoDB
Query Optimization in MongoDBQuery Optimization in MongoDB
Query Optimization in MongoDB
 
Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27
 
Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26
 
Mongo db a deep dive of mongodb indexes
Mongo db  a deep dive of mongodb indexesMongo db  a deep dive of mongodb indexes
Mongo db a deep dive of mongodb indexes
 
Indexing and Query Optimizer
Indexing and Query OptimizerIndexing and Query Optimizer
Indexing and Query Optimizer
 
About elasticsearch
About elasticsearchAbout elasticsearch
About elasticsearch
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling
 
Mongo indexes
Mongo indexesMongo indexes
Mongo indexes
 
Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)
 
Test driving Azure Search and DocumentDB
Test driving Azure Search and DocumentDBTest driving Azure Search and DocumentDB
Test driving Azure Search and DocumentDB
 
An Introduction to Elastic Search.
An Introduction to Elastic Search.An Introduction to Elastic Search.
An Introduction to Elastic Search.
 
Automated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index RobotAutomated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index Robot
 
Indexing In MongoDB
Indexing In MongoDBIndexing In MongoDB
Indexing In MongoDB
 
Indexing and Query Optimisation
Indexing and Query OptimisationIndexing and Query Optimisation
Indexing and Query Optimisation
 
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
 
2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongo2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongo
 

KĂźrzlich hochgeladen

(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...shivangimorya083
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 

KĂźrzlich hochgeladen (20)

(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 

MongoDB Indexes Guide

  • 1. MongoDB and Indexes Doug Duncan dugdun@hotmail.com @dugdun
  • 2. What we’ll cover • What are indexes? • Types • Properties • Why use indexes? • How to create indexes. • Commands to check indexes and plans.
  • 4. What are indexes? Indexes are special data-structures that store a subset of your data in an easily traversable format. MongoDB stores indexes in a b-tree format which allows for efficient access to the index content. Proper index use is good and makes a system run optimally. Improper index use can bring a system to a grinding halt.
  • 5. What are indexes? Indexes are stored similar in a format similar to the following if there was an index on Origin: [ABE] -> 0xa193b48c [ABE] -> 0x8e8b242a [ABE] -> 0x0928cdc1 … [DEN] -> 0x24aa4ecd [DEN] -> 0x87396a3c [DEN] -> 0x9392ab2f … [LAX] -> 0x89ccede0 …
  • 6. Types of indexes • _id • Simple • Compound • Multikey • Full-Text • Geo-spatial • Hashed
  • 7. The _id index • The _id index is automatically created and cannot be removed. • This is the same as a primary key in traditional RDBMS. • Default value is a 12-byte ObjectId: • 4-byte time stamp • 3-byte machine id • 2-byte process id • 3-byte counter
  • 8. Simple index • A simple index is an index on a single key • This is similar to a book’s index where you look up a word to find the pages it’s referenced on.
  • 9. Compound index • A compound index is created over two or more fields in a document • This is similar to a phone book where you can find the phone number of a person given their first and last names.
  • 10. Multikey index • A multikey index is an index that’s created on a field that contains an array. • If using in a compound index, only a single field in a given document can be an array. • You will get one entry in the index for every item in the array for the given document. This means if you have an array with 100 items, that document will have 100 index entries.
  • 11. Full-text index • This is an index over a text based field, similar to how Google indexes web pages.
  • 12. Geo-spatial index • A geo-spatial index will allow you to determine distance from a given point. • Works on both planar and spherical geometries.
  • 13. Hashed indexes • A hashed index is used in hash based sharding, and allows for a more randomized distribution. • Hashed indexes cannot contain compound keys or be unique. • Hashed indexes can contain the key in both a hashed and non-hashed version. The non- hashed version will allow for range based queries.
  • 14. Index properties • Unique • Sparse • TTL • Partial (new in 3.2)
  • 15. Unique • The unique property allows for only a single value for the indexed field, or combination of fields for a compound index db.collection.createIndex({“email”: 1}, {“unique”: true}) • A unique index can only have a single null or missing field value for all documents in the collection.
  • 16. Sparse • The sparse property allows you to index only documents that contain a value for the given field. db.collection.createIndex({“kids”: 1}, {“sparse”: true}) • A sparse index will not be used if it would result in an incomplete result set, unless specifically hinted. db.collection.find({“kids”: {“$gte”: 5})
  • 17. TTL • The TTL property allows for the automatic removal of documents after a given time period. db.collection.createIndex({“accessTime”: 1}, {“expireAfterSeconds”: “1200”}) • The indexed field should contain an ISODate() value. If any other type is used the document will not be removed. • The TTL removal process runs once every 60 seconds so you might see the document even though the time has expired.
  • 18. Partial • The partial property allows you to index a subset of your data. db.collection.createIndex({“movie”: 1, “reviews”: 1}, {“rating”: {“$gte”: 4}}) • The index will not be used if it would provide an incomplete result set (similar to the sparse index).
  • 20. Why use indexes? • Efficiently retrieving document matches • Equality matching • Inequality or range matching • Sorting • Lack of a usable index will cause MongoDB to scan the entire collection.
  • 21. How to create indexes.
  • 22. Before creating indexes • Think about the queries you will be running and try to create as few indexes as possible to support those queries. Similar query patterns could use the same (or very similar) indexes. • Think about the data that you will query and put your highly selective fields first in the index if possible. • Check your current indexes before creating new ones. MongoDB will allow you to create indexes with the same fields in different orders.
  • 23. Simple indexes • When creating a simple index, the sort order, ascending (1) or descending (-1), of the values doesn’t matter as much as MongoDB can walk the index forwards and backwards. • Simple index creation: db.flights.createIndex({“Origin”: 1})
  • 24. Compound indexes • When creating a compound index, the sort order, ascending (1) or descending (-1), of the values starts to matter, especially if the index is used to sort on multiple keys. • When creating compound indexes you want to add keys to the index in the following key order: • Equality matches • Sort fields • Inequality matches • A compound index will also help any queries that are made based off the left most subset of keys.
  • 25. Compound indexes • Compound index creation: db.flights.createIndex({“Origin”: 1, “Dest”: 1, “FlightDate”: -1}) • Queries supported: db.flights.find({“Origin”: “DEN”}) db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”}) db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”}).sort({“FlightDate”: -1}) db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”}).sort({“FlightDate”: 1})
  • 26. Compound indexes • An index created as follows: db.flights.createIndex({“Origin”: 1, “Dest”: -1}) Could be used with either of the following queries as well since MongoDB can walk the index either way: db.flights.find().sort({“Origin”: 1, “Dest”: -1}) db.flights.find().sort({“Origin”: -1, “Dest”: 1})
  • 27. Full-text indexes • Full-text index creation: • db.messages.createIndex({“body”: “text”}) • To search using the index finding any of the words: db.messages.find({“$text”: {“$search”: “some text”}}) • To search using the index finding a phrase db.message.find({“$text”: {“$search”: “”some text””}}
  • 28. Covering indexes • Covering indexes are indexes that will answer a query without going back to the data. For example: db.flights.createIndex({“Origin”: 1, “Dest”: 1, “ArrDelay”: 1, “UniqueCarrier”: 1}) • The following query would be covered as all fields are in the index: db.flights.find({“Origin”: “DEN”, “Dest”: “JFK”}, {“UniqueCarrier”: 1, “ArrDelay”: 1, “_id”: 0}).sort({“ArrDelay”: -1})
  • 29. Indexing nested fields/documents • Let’s say you have documents with nested documents in them like the following: db.locations.findOne() { “_id”: ObjectId(…), …, “location”: { “state”: “Colorado”, “city”: “Lyons” } }
  • 30. Indexing nested fields/documents • You can index on embedded fields by using dot notation: db.locations.createIndex({“location.state”: 1})
  • 31. Indexing nested fields/documents • You can also index embedded documents db.locations.createIndex({“location”: 1}) • If you do this the query must match the document exactly (keys in the same order). That means that this will return the document: db.locations.find({“location”: {“state”: “Colorado”, “city”: “Lyons”}) • But this won’t: db.locations.find({“location”: {“city”: “Lyons”, “state”: “Colorado”})
  • 32. Index Intersection • Index intersection is when MongoDB uses two or more indexes to satisfy a query. • Given the following two indexes: db.orders.createIndex({“qty”: 1}) db.orders.createIndex({“item”: 1}) • Index intersection means a query such as the following could use both indexes in parallel with the results being merged together to satisfy the query: db.orders.find({“item”: “ABC123”, “qty”: {“$gte”: 15}})
  • 33. Indexing arrays • You can index fields that contain arrays as well. • Compound indexes however can only have a single field that is an array in a given document. If a document has two indexed fields that are arrays, you will get an error. db.arrtest.createIndex({“a”: 1, “b”: 1}) db.arrtest.insert({"b": [1,2,3], "a": [1,2,3]}) cannot index parallel arrays [b] [a] WriteResult({ "nInserted": 0, "writeError": { "code": 10088, "errmsg": "cannot index parallel arrays [b] [a]" } })
  • 34. Index Intersection • Index intersection is when MongoDB uses two or more indexes to satisfy a query. • Given the following two indexes: db.orders.createIndex({“qty”: 1}) db.orders.createIndex({“item”: 1}) • Index intersection means a query such as the following could in theory use both indexes in parallel with the results being merged together to satisfy the query: db.orders.find({“item”: “ABC123”, “qty”: {“$gte”: 15}})
  • 35. Removing indexes • The command to remove indexes is similar to the one to create the index. db.flights.dropIndex({“Origin”: 1, “Dest”: -1})
  • 36. Commands to check indexes and index usage
  • 37. View all indexes in a database • To view all indexes in a database use the following command: db.system.indexes.find() • For each index you’ll see the fields the index was created with, the name of the index and the namespace (db.collection) that the index was built on.
  • 38. View indexes for a given collection • To view all indexes for a given collection use the following command: db.collection.getIndexes() • This returns the same information as the previous command, but is limited to the given collection.
  • 39. View index sizes • To view the size of all indexes in a collection: db.collection.stats() • You will see the size of all indexes and the size of each individual index in the results. The sizes are in bytes.
  • 40. How to see if an index is used • If you want to see if an index is used, append the .explain() operator to your query db.flights.find({“Origin”: “DEN”}).explain() • The explain operator has three levels of verbosity: • queryPlanner - this is the default, and it returns the winning query plan • executionStats - adds execution stats for the plan • allPlansExecution - adds stats for the other candidate plans
  • 41. Notes on indexes. • When creating an index you need to know your data and the queries that will run against it. • Don’t build indexes in isolation! • While indexes can improve performance, be careful to not over index as every index gets updated every time you write to the collection.
  • 42. Q & A
  • 43. End Notes • User group discounts • Manning publications: www.manning.com • Code ‘ug367’ to save 36% off order • APress publications: www.appress.com • Code ‘UserGroup’ to save 10% off order • O’Reilly publication: www.oreilly.com • Still waiting to get information
  • 44. End Notes • Communication • Twitter: @MUGDenver and #MUGDenver • Email: mugdenver@gmail.com • Slack: ???
  • 45. End Notes • MongoDB World • When: June 28th and 29th • Where: NYC • Save 25% by using code ‘DDuncan’

Hinweis der Redaktion

  1. The indexes do not have to store the field names as all fields are the same for each entry. After the values you will have a pointer back to the data portion of the file.
  2. _id index is a primary key. Default value is a 12 byte ObjectId that has as it’s first 4 bytes a time stamp that the document was entered into the collection. 3 byte machine id, 2 byte process id and 3 byte counter starting with a random value. You can however override this as long as the values you enter are unique. Automatically created and cannot be removed. Simple index is similar to a book where you look up a word and find page numbers. Compound index is similar to a phone book where you can find the phone number of a person if you know their first and last names. Multikey indexes are indexes over columns that have an array. There will be an entry for each item in the array and there can only be a single array column indexed in a given index. Full-text indexes are similar to what Google does when search for words in a web site. Geo-spatial indexes allow you to determine map proximity similar to Google maps find restaurants around this location. Can use both 2d for planar geometry and 2dsphere for spherical geometries. Hashed indexes are used in hash-based sharding which allows for a more random distribution. Can only do equality searches against this type of index, unless you add the field as in both hashed and non-hashed forms ({“field”: “hashed”, “field”: 1}). Cannot be a compound or unique index.
  3. Unique indexes are indexes that can only store a single value for the given key that’s being indexes (or set of values if a compound index). This can only contain a single document that has a null for the indexed field or a document that doesn’t have the field at all. Cannot have both of these. db.coll.ensureIndex({“a”: 1}, {“unique”: true}). Sparse indexes only index the documents that the field actually exists in. Will not index missing fields, but will index fields whose value is null. db.coll.ensureIndex({“a”: 1}, {“sparse”: true}). Use db.coll.find().hint({“a”: 1}) to see what index contains. In 2.6 and earlier, this could result in queries returning incorrect data. TTL indexes are indexes that will automatically remove documents in a collection after a given time. Indexed field needs to be a Date object. db.coll.ensureIndex({“field1”: 1}, {“expireAfterSeconds”: 300}). If you put any other value in the indexed field it will never expire. Partial indexes allow you to add a filter to the index so only those documents are indexes. This allows you to have smaller storage footprint than a regular index over the same field. These should be preferred over sparse indexes. db.coll.createIndex({“a”: 1}, {“partialFilterExpression”: {“a”: {“$gt”: 5}}}). Mongo will not use this index if it will return an incomplete result set. The query must contain the filter expression or a modified version that will return a smaller subset of the documents covered by the index.
  4. I’ve never had a case where index intersection worked, at least not when running an explain() on the query.