SlideShare a Scribd company logo
Indexing Documents in MongoDBAlberto LernerSoftware Engineer – 10Genalerner@10gen.com
Indexing BasicsMongoDB can use separate tree structures to index a collectionWhen processing a search criteria, MongoDB will try to avoid going through a collection, taking advantage of existing indices
Team Work MongoDB’s job: use an index, if possibleSearchCriteriausing indexscanning the collection
Your JobTo provide indices for important queriesImportant queries?Very frequently usedEspecially low response time required
Creating an IndexYou have an automatic one over _idOthers can be created with ‘ensureIndex’# index over attribute ‘name’db.<collection>.ensureIndex({name:1})# compound keys, ascending/descendingdb.<collection>.ensureIndex({name:1, date:-1 })# unique keysdb.<collection>.ensureIndex({sku:1}, {unique:true})# building in backgrounddb.<collection>.ensureIndex( …, {background:true})
Simple Search CriteriaSearch criteria is the index key or a prefix thereofdb.<collection>.find({sku:1234}) # index over skudb.<collection>.find({sku:1234}) # index over sku, <xxx>
More Exact Matching# index over sku….find({sku: {$in:[1234,5678])# index over ‘product.sku’….find({“product.sku”:1234})# a tricky query, would need index on ‘product’ instead….find({product: {sku:1234}}) { _id:1, product: {sku:1234} } # matches
Range CriteriaSearch criteria may return several resultsdb.<collection>.findOne({sku: {$gt:1234}}) db.<collection>.find({sku: {$gt:5678,$lt:5699}})
Range Criteria (cont)# index over sku….find({sku:/^12/}
Other Operations# index over sku….update({sku:1234},{$inc:{sold:1}}})….remove({sku:1234})
Index CoveringSometimes, all the needed information is in the index itself….count({color:blue}) # index over color….find({sku:1234},{color:1}) # index over sku, color
Missing fieldsAll documents have an entry on an indexA missing field is indexed as a NULL# matches all documents without sku# if index over sku is unique, there could be only one….find({sku:NULL}) # will be using a sku index, but not there yet….find({{sku:{$exits:true}})
Array MatchingA field that contains an array will have one entry in the index per element in the array{ _id: “abcd”, x:[2,10]} will appear in all the following queries using an index over x….find({x:2})….find({x:10})….find({x:[2,10]})….find({x:{$gt:5}}) # because of 10
Indexes and OrderingSort elimination is also accomplished though using indexes….find({sku:{$gt:56678}).sort({sku:1})….find().sort({sku:-1})  # can traverse backwards
Is It Using the Index?explain() tool allows you to see whether an index is being chosendb.<collection>.find({sku:{$gt:5}}).explain(){“cursor” : “BtreeCursor sku_1”,…}
HintingSometimes we may force or avoid the use of an indexUsually, it should not be necessary to intervene# forces use of index over sku….find{{sku:1, …}).hint({sku:1})# prevents any index to be used….find({sku:1,…}).hint({$natural:1})
When Indexes Don’t Help# negation….find({sku:{$ne:9876}})# index helps to filter string sku’s, though….find({sku:/88/}) # generic regex# $where may contain very expressive searches# we don’t even try….find({$where:”this.sku==1234”})
Many indices?Evaluating search criteria currently uses just one index, even if more than one would be possibleThe choice is based on previous executions; if an index “worked well” for a query before, it’ll likely be againException: $or can use more than one index
So When to Index?There’s a trade off between search criteria efficiency and insertion/update/deletion of keysAlso, there is (a quite high) limit on number of indexes per collection (that we keep bumping up)
Indexes ResourcesIndexes are memory mapped as well; Be mindful of number of indexes and choice of keys# In ‘indexSizes’, individual indexes in collectiondb.<collection>.stats()# All indexes in collectiondb.<collection>.TotalIndexSize()
Take awayThe picture to keep in mindSearchCriteria
Questions?www.mongodb.org

More Related Content

PPTX
Indexing and Query Optimization
PPTX
Drupal 8 database api
PPTX
MySql:Introduction
PPTX
MySql:Basics
PDF
JavaScript Fundamentals with Angular and Lodash
PDF
Lodash js
PDF
Chaining and function composition with lodash / underscore
PPTX
Indexing and Query Optimisation
Indexing and Query Optimization
Drupal 8 database api
MySql:Introduction
MySql:Basics
JavaScript Fundamentals with Angular and Lodash
Lodash js
Chaining and function composition with lodash / underscore
Indexing and Query Optimisation

What's hot (20)

PDF
Database API, your new friend
DOCX
Java and xml
PPTX
Being! Black Hat with Drupal - Anand Toshniwal
PPTX
Drupal7 dbtng
PPTX
Slick: Bringing Scala’s Powerful Features to Your Database Access
PDF
From mysql to MongoDB(MongoDB2011北京交流会)
PDF
Functional Core, Reactive Shell
PDF
Functions
PDF
ESNext for humans - LvivJS 16 August 2014
PDF
Redis the better NoSQL
PDF
PPTX
Indexing and Query Optimization
PPTX
Webinar: Indexing and Query Optimization
PDF
MongoDB With Style
PDF
Laravel doctrine
PDF
Brief introduction of Slick
PPT
Django-Queryset
ODP
2011 Mongo FR - Indexing in MongoDB
KEY
Node js mongodriver
PPTX
SenchaCon 2016: Theming the Modern Toolkit - Phil Guerrant
Database API, your new friend
Java and xml
Being! Black Hat with Drupal - Anand Toshniwal
Drupal7 dbtng
Slick: Bringing Scala’s Powerful Features to Your Database Access
From mysql to MongoDB(MongoDB2011北京交流会)
Functional Core, Reactive Shell
Functions
ESNext for humans - LvivJS 16 August 2014
Redis the better NoSQL
Indexing and Query Optimization
Webinar: Indexing and Query Optimization
MongoDB With Style
Laravel doctrine
Brief introduction of Slick
Django-Queryset
2011 Mongo FR - Indexing in MongoDB
Node js mongodriver
SenchaCon 2016: Theming the Modern Toolkit - Phil Guerrant
Ad

Similar to Indexing documents (20)

PDF
Mongoseattle indexing-2010-07-27
PDF
Indexing and Query Optimizer (Richard Kreuter)
PPTX
Indexing with MongoDB
PDF
Indexing and Query Optimizer (Mongo Austin)
PDF
Mongophilly indexing-2011-04-26
PDF
Indexing and Query Optimizer
PPT
Indexing & query optimization
PPTX
MongoDB (Advanced)
PPTX
Indexing Strategies to Help You Scale
PPTX
Indexing and Query Optimisation
PPTX
Indexing In MongoDB
DOCX
unit 4,Indexes in database.docx
PPTX
Indexing & Query Optimization
PPTX
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
PDF
Indexing
PPTX
MongoDB and Indexes - MUG Denver - 20160329
PDF
Practical Google App Engine Applications In Py
PPTX
Webinar: Applikationsentwicklung mit MongoDB : Teil 5: Reporting & Aggregation
PPT
Fast querying indexing for performance (4)
ODP
A Year With MongoDB: The Tips
Mongoseattle indexing-2010-07-27
Indexing and Query Optimizer (Richard Kreuter)
Indexing with MongoDB
Indexing and Query Optimizer (Mongo Austin)
Mongophilly indexing-2011-04-26
Indexing and Query Optimizer
Indexing & query optimization
MongoDB (Advanced)
Indexing Strategies to Help You Scale
Indexing and Query Optimisation
Indexing In MongoDB
unit 4,Indexes in database.docx
Indexing & Query Optimization
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
Indexing
MongoDB and Indexes - MUG Denver - 20160329
Practical Google App Engine Applications In Py
Webinar: Applikationsentwicklung mit MongoDB : Teil 5: Reporting & Aggregation
Fast querying indexing for performance (4)
A Year With MongoDB: The Tips
Ad

More from MongoDB (20)

PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
PDF
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
PDF
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
PDF
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
PDF
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
PDF
MongoDB SoCal 2020: MongoDB Atlas Jump Start
PDF
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
PDF
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
PDF
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
PDF
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
PDF
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
PDF
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
PDF
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
PDF
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
PDF
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
PDF
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Cloud computing and distributed systems.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
KodekX | Application Modernization Development
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
cuic standard and advanced reporting.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Approach and Philosophy of On baking technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Big Data Technologies - Introduction.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Cloud computing and distributed systems.
Review of recent advances in non-invasive hemoglobin estimation
Chapter 3 Spatial Domain Image Processing.pdf
Network Security Unit 5.pdf for BCA BBA.
Programs and apps: productivity, graphics, security and other tools
KodekX | Application Modernization Development
“AI and Expert System Decision Support & Business Intelligence Systems”
The AUB Centre for AI in Media Proposal.docx
cuic standard and advanced reporting.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Approach and Philosophy of On baking technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectroscopy.pptx food analysis technology
Unlocking AI with Model Context Protocol (MCP)
Big Data Technologies - Introduction.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Understanding_Digital_Forensics_Presentation.pptx

Indexing documents

  • 1. Indexing Documents in MongoDBAlberto LernerSoftware Engineer – 10Genalerner@10gen.com
  • 2. Indexing BasicsMongoDB can use separate tree structures to index a collectionWhen processing a search criteria, MongoDB will try to avoid going through a collection, taking advantage of existing indices
  • 3. Team Work MongoDB’s job: use an index, if possibleSearchCriteriausing indexscanning the collection
  • 4. Your JobTo provide indices for important queriesImportant queries?Very frequently usedEspecially low response time required
  • 5. Creating an IndexYou have an automatic one over _idOthers can be created with ‘ensureIndex’# index over attribute ‘name’db.<collection>.ensureIndex({name:1})# compound keys, ascending/descendingdb.<collection>.ensureIndex({name:1, date:-1 })# unique keysdb.<collection>.ensureIndex({sku:1}, {unique:true})# building in backgrounddb.<collection>.ensureIndex( …, {background:true})
  • 6. Simple Search CriteriaSearch criteria is the index key or a prefix thereofdb.<collection>.find({sku:1234}) # index over skudb.<collection>.find({sku:1234}) # index over sku, <xxx>
  • 7. More Exact Matching# index over sku….find({sku: {$in:[1234,5678])# index over ‘product.sku’….find({“product.sku”:1234})# a tricky query, would need index on ‘product’ instead….find({product: {sku:1234}}) { _id:1, product: {sku:1234} } # matches
  • 8. Range CriteriaSearch criteria may return several resultsdb.<collection>.findOne({sku: {$gt:1234}}) db.<collection>.find({sku: {$gt:5678,$lt:5699}})
  • 9. Range Criteria (cont)# index over sku….find({sku:/^12/}
  • 10. Other Operations# index over sku….update({sku:1234},{$inc:{sold:1}}})….remove({sku:1234})
  • 11. Index CoveringSometimes, all the needed information is in the index itself….count({color:blue}) # index over color….find({sku:1234},{color:1}) # index over sku, color
  • 12. Missing fieldsAll documents have an entry on an indexA missing field is indexed as a NULL# matches all documents without sku# if index over sku is unique, there could be only one….find({sku:NULL}) # will be using a sku index, but not there yet….find({{sku:{$exits:true}})
  • 13. Array MatchingA field that contains an array will have one entry in the index per element in the array{ _id: “abcd”, x:[2,10]} will appear in all the following queries using an index over x….find({x:2})….find({x:10})….find({x:[2,10]})….find({x:{$gt:5}}) # because of 10
  • 14. Indexes and OrderingSort elimination is also accomplished though using indexes….find({sku:{$gt:56678}).sort({sku:1})….find().sort({sku:-1}) # can traverse backwards
  • 15. Is It Using the Index?explain() tool allows you to see whether an index is being chosendb.<collection>.find({sku:{$gt:5}}).explain(){“cursor” : “BtreeCursor sku_1”,…}
  • 16. HintingSometimes we may force or avoid the use of an indexUsually, it should not be necessary to intervene# forces use of index over sku….find{{sku:1, …}).hint({sku:1})# prevents any index to be used….find({sku:1,…}).hint({$natural:1})
  • 17. When Indexes Don’t Help# negation….find({sku:{$ne:9876}})# index helps to filter string sku’s, though….find({sku:/88/}) # generic regex# $where may contain very expressive searches# we don’t even try….find({$where:”this.sku==1234”})
  • 18. Many indices?Evaluating search criteria currently uses just one index, even if more than one would be possibleThe choice is based on previous executions; if an index “worked well” for a query before, it’ll likely be againException: $or can use more than one index
  • 19. So When to Index?There’s a trade off between search criteria efficiency and insertion/update/deletion of keysAlso, there is (a quite high) limit on number of indexes per collection (that we keep bumping up)
  • 20. Indexes ResourcesIndexes are memory mapped as well; Be mindful of number of indexes and choice of keys# In ‘indexSizes’, individual indexes in collectiondb.<collection>.stats()# All indexes in collectiondb.<collection>.TotalIndexSize()
  • 21. Take awayThe picture to keep in mindSearchCriteria