SlideShare a Scribd company logo
Using MongoDB and Python
Michael Bright
Python User Group, Grenoble
23 Fevrier 2016
Databases
Databases
Relational Databases (RDBMS) “Les SGBDRs” (OLTP – Online Transaction Processing)
• Based on relational database model invented by E.F. Codd (IBM) in 1970
• SQL (SEQUEL): expressive query language
• Achieves performance increase through “vertical scaling” (higher performance node)
NoSQL (No SQL, or “Not just SQL”?) existed before RDBMS, resurged out of a need for more Scalability for
Web2.0
- Columnar, e.g. Cassandra, Hbase, Vertica
- Document Oriented, e.g. CouchDB, MongoDB, RethinkDB
- Graph, e.g. Neo4J
- Key-value, e.g. CouchDB, Dynamo, Riak, Redis, Oracle NoSQL, MUMPS
- Multi-model, e.g. Alchemy, ArangoDB
• Achieves performance increase through “horizontal scaling” (by adding nodes)
OLAP – Online Analytic Processing
Databases for scalability
The need for scalability requires distribution of processing making it more difficult
to satisfy all criteria, according to the CAP Theorem (2000)
- Consistency (Atomicity)
- Availability (A request will always receive a response)
- Partition Tolerance (if nodes become disconnected, the system continues to
function)
It is impossible to satisfy all three constraints (proved by MIT, 2002).
RDBMS - favour Consistency and Availability but cannot scale horizontally
NoSQL - favour Consistency and Partitioning or Availability and Partitioning
Relational vs. NoSQL
Relational databases provide
- Rich query language (SQL)
- Strong consistency
- Secondary indices
NoSQL provides
- Flexibility
- “dynamic schema” allows missing or extra fields, embedded structures
- Scalability
- Adding nodes allows to scale data size
- Performance
- Adding nodes allows to scale performance
MongoDB combines both sets of advantages
MongoDB
MongoDB – a Document-Oriented DB
Open source DB on github with commercial support from MongoDB Inc (was 10gen).
• Dynamic schema (schema-less) aids agile development
• A document is an associative array (possibly more than 1-level deep), e.g. JSON Object
• Uses BSON (binary JSON format) for serialization (easy to parse)
• Binaries downloadable for Linux(es), Windows, OSX, Solaris
• Drivers available in many languages
• Provides an aggregation framework
• Is the “M” in MEAN
MongoDB – Performance
• Written in C++
• It achieves high performance, especially on dynamic queries
• Allows primary & secondary indices (main tuning element)
• Is horizontally scalable across many nodes
• Extensive use of memory-mapped files
i.e. read-through write-through memory caching.
• Provides high availability with Master/slave replication
• Provides Sharding (horizontal partitioning of data across nodes)
• Horizontal partitioning : complete documents (or rows) are stored on a node
• Vertical partitioning: documents (or rows) are split across nodes
MongoDB – Architecture
MongoDB – Terminology
RDBMS MongoDB
Table, View ➜ Collection
Row ➜ Document
Index ➜ Index
Join ➜ Embedded Document
Foreign Key ➜ Reference
Partition ➜ Shard
MongoDB – Enterprise Products
There are supported products for the enterprise
• MongoDB Enterprise: for production workloads
• MongoDB Compass: Data and Schema visualization tool
• MongoDB Connector for BI:
Allows users to visualize their MongoDB Enterprise data using existing relational
business intelligence tools such as Tableau.
Foreign data wrapper with PostgreSQL to provide a relational SQL view on
MongoDB data
MongoDB v3.2: new features
• The default storage engine is now WiredTiger
• Following acquisition of WiredTiger, Inc.
• Introduced as an optional engine in MongoDB 3.0
• Has more granular concurrency control and native compression (lowering storage costs,
increasing h/w utilization, throughput and providing more predictable performance)
• Replication election enhancements
• Config servers as replica sets
• readConcern, and document validations.
• OpsManager 2.0
MongoDB v3.2: new aggregation features
• $sample, $lookup
• $indexStats
• $filter,$slice,$arrayElemAt, $isArray, $concatArrays
• Partial Indexes
• Document Validation
• 4 bit testing operators
• 2 new Accumulators for $group
• 10 new Arithmetic expression operators
MongoDB components
Components
mongod - The database daemon process.
mongos - Sharding controller.
mongo - The database shell (uses interactive javascript).
Utilities
mongodump - MongoDB dump tool - for backups, snapshots, etc.
mongorestore - MongoDB restore a dump
mongoexport - Export a single collection to test (JSON, CSV)
mongoimport - Import from JSON or CSV
mongofiles - Utility for putting and getting files from MongoDB GridFS
mongostat - Show performance statistics
MongoDB University (http://university.mongodb.com/)
MongoDB drivers exist for many languages
12 official drivers, plus many community languages
MongoDB docs: (https://docs.mongodb.org/manual/)
Using MongoDB
MongoDB shell
> mongo
MongoDB shell version: 3.2.3
connecting to: test
> show dbs
Money_UK 0.000GB
aggregation_example 0.000GB
local 0.000GB
test 0.000GB
> use Money_UK
switched to db Money_UK
> show collections
CARD
SAVING
MongoDB shell
> use test
switched to db test
> db.newcoll.insert({ 'field1': 'an example' })
WriteResult({ "nInserted" : 1 })
> db.newcoll.find().count()
1
> db.newcoll.find()
{ "_id" : ObjectId("56cc7446bb127b86163cf226"), "field1" : "an example" }
> db.newcoll.findOne()
{ "_id" : ObjectId("56cc7446bb127b86163cf226"), "field1" : "an example" }
> db.newcoll.find({'_id':ObjectId("56cc7446bb127b86163cf226")})
{ "_id" : ObjectId("56cc7446bb127b86163cf226"), "field1" : "an example" }
MongoDB shell
> var doc2={'field1': 'example', 'field2': 'example2'}
> db.newcoll.insert(doc2)
WriteResult({ "nInserted" : 1 })
> db.newcoll.find().count()
2
> db.newcoll.find().pretty()
{ "_id" : ObjectId("56cc7446bb127b86163cf226"), "field1" : "an example"
}
{
"_id" : ObjectId("56cc7711bb127b86163cf227"),
"field1" : "example",
"field2" : "example2"
}
MongoDB indexing
Indexing is the single biggest tunable performance factor.
Can index on a single field, e.g. on ascending or descending values:
db.newcoll.ensureIndex( { ‘field1’: 1 } ) // or -1: descending
Or subfields:
db.newcoll.ensureIndex( { ‘arr.subfield’: 1 } )
Or multiple fields:
db.newcoll.ensureIndex( { ‘arr.subfield’: 1, ‘field2’: -1 } )
MongoDB indexing - 2
It is also possible to provide hints to the indexer, on uniqueness, sparseness or to
request index creation as a background task, e.g.
db.newcoll.ensureIndex( { ‘field1’: 1 }, { ‘unique’: true} )
db.newcoll.ensureIndex( { ‘field1’: 1 , ‘field2’: 1}, { ‘sparse’: true} )
db.newcoll.ensureIndex( { ‘field1’: 1 }, { ‘background’: true} )
MongoDB searching
We can search based on a single field
db.newcoll.find( { ‘field1’: ‘any match’ } )
Or subfields:
db.newcoll.find( { ‘arr.subfield’: ‘sub field match’ } )
Or multiple fields:
db.newcoll.find( { ‘arr.subfield’: ‘sub field match’, ‘field2’: ‘match2’ } )
Available indices will be used if possible
MongoDB sorting
We can sort the results in ascending or descending order
db.newcoll.find().sort( { ‘field1’: 1 } ) // or -1: descending
or
db.newcoll.find().sort( { ‘field1’: 1, ‘field2’: -1 } )
Available indices will be used if possible
MongoDB projections
We can reduce the number of returned fields (the projection).
This may enable data access directly from the index.
In this example we limit the returned fields to ‘field1’ (and the ‘_id’ index).
db.newcoll.find({ ‘field1’: ‘example’ }, { ‘field1’: 1} )
In this example we limit the returned fields to ‘field1’ (without the ‘_id’ index).
db.newcoll.find({ ‘field1’: ‘example’ }, { ‘_id’: 0, ‘field1’: 1} )
We can also override the default index
db.newcoll.find({ ‘field1’: ‘example’ }, { ‘_id’: ‘field1’ } )
MongoDB explain plan
We can request explanation of how a query will be handled (showing possible index use)
> db.newcoll.find().explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.newcoll",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [ ]
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"$and" : [ ]
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "MJBRIGHT7",
"port" : 27017,
"version" : "3.2.3",
"gitVersion" : "b326ba837cf6f49d65c2f85e1b70f6f31ece7937"
},
"ok" : 1
}
MongoDB special index types
• Geospatial Indexes (2d Sphere)
• Text Indexes
• TTL Collections (expireAfterSeconds)
• Hashed Indexes for sharding
MongoDB aggregation framework
Aggregation uses a pipeline of operations amongst
• $match
• $project
• $unwind
• $group
• $sort
MongoDB aggregation example
pipeline = [
{ "$project": { // Select fields of interest
'year': { "$dateToString": { 'format': "%Y", 'date': "$date" } },
'tags': 1, 'value': 1, }, },
{ "$match": { 'year': str(year) }},
{ "$group": {"_id": "$tags", "total": {"$sum": {"$abs": "$value"}}} },
{ "$sort": SON([("total", -1), ("_id", -1)]) }
]
cursor = db.collection.aggregate(pipeline)
MongoDB aggregation example
pipeline = [
{ "$project": {
'year': { "$dateToString": { 'format': "%Y", 'date': "$date" } },
'tags': 1, 'value': 1, }, },
{ "$match": { 'year': str(year) }}, // match on fields
{ "$group": {"_id": "$tags", "total": {"$sum": {"$abs": "$value"}}} },
{ "$sort": SON([("total", -1), ("_id", -1)]) }
]
cursor = db.collection.aggregate(pipeline)
MongoDB aggregation example
pipeline = [
{ "$project": {
'year': { "$dateToString": { 'format': "%Y", 'date': "$date" } },
'tags': 1, 'value': 1, }, },
{ "$match": { 'year': str(year) }},
{ "$group": {"_id": "$tags", "total": {"$sum": {"$abs": "$value"}}} }, // ‘reduce’
{ "$sort": SON([("total", -1), ("_id", -1)]) }
]
cursor = db.collection.aggregate(pipeline)
MongoDB aggregation example
pipeline = [
{ "$project": {
'year': { "$dateToString": { 'format': "%Y", 'date': "$date" } },
'tags': 1, 'value': 1, }, },
{ "$match": { 'year': str(year) }},
{ "$group": {"_id": "$tags", "total": {"$sum": {"$abs": "$value"}}} },
{ "$sort": SON([("total", -1), ("_id", -1)]) } // sort the results
]
cursor = db.collection.aggregate(pipeline)
PyMongo – ca y est, on arrive !
But first …
PyMongo – ca y est, on arrive !
… there are alternatives to the official PyMongo driver.
• Motor is a full-featured, non-blocking MongoDB driver for Python Tornado
applications.
• TxMongo is an asynchronous Twisted Python driver for MongoDB.
PyMongo – ca y est, on arrive !
… and other Python/MongoDB tools
ORM-like Layers
New users are recommended to begin with PyMongo.
Nevertheless other implementations are available providing higher abstraction.
Humongolus, MongoKit, Ming, MongoAlchemy, MongoEngine, Minimongo, Manga,
MotorEngine
Framework Tools
This section lists tools and adapters that have been designed to work with various Python
frameworks and libraries.
Django MongoDB Engine, Mango, Django MongoEngine, mongodb_beaker, Log4Mongo,
MongoLog, C5t, rod.recipe.mongodb, repoze-what-plugins-mongodb, Mongobox, Flask-
MongoAlchemy, Flask-MongoKit, Flask-PyMongo.
Questions?
Backup Slides

More Related Content

What's hot

Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
MongoDB - Aggregation Pipeline
MongoDB - Aggregation PipelineMongoDB - Aggregation Pipeline
MongoDB - Aggregation Pipeline
Jason Terpko
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
Steven Francia
 
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
MongoDB
 
Back to Basics Webinar 5: Introduction to the Aggregation Framework
Back to Basics Webinar 5: Introduction to the Aggregation FrameworkBack to Basics Webinar 5: Introduction to the Aggregation Framework
Back to Basics Webinar 5: Introduction to the Aggregation Framework
MongoDB
 
Aggregation Framework in MongoDB Overview Part-1
Aggregation Framework in MongoDB Overview Part-1Aggregation Framework in MongoDB Overview Part-1
Aggregation Framework in MongoDB Overview Part-1
Anuj Jain
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
Caserta
 
The Aggregation Framework
The Aggregation FrameworkThe Aggregation Framework
The Aggregation FrameworkMongoDB
 
Getting Started with MongoDB and NodeJS
Getting Started with MongoDB and NodeJSGetting Started with MongoDB and NodeJS
Getting Started with MongoDB and NodeJS
MongoDB
 
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop ConnectorAnalytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Henrik Ingo
 
Webinar: Data Processing and Aggregation Options
Webinar: Data Processing and Aggregation OptionsWebinar: Data Processing and Aggregation Options
Webinar: Data Processing and Aggregation Options
MongoDB
 
Aggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days MunichAggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days Munich
Norberto Leite
 
Aggregation in MongoDB
Aggregation in MongoDBAggregation in MongoDB
Aggregation in MongoDBKishor Parkhe
 
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
MongoDB: Comparing WiredTiger In-Memory Engine to RedisMongoDB: Comparing WiredTiger In-Memory Engine to Redis
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
Jason Terpko
 
Back to Basics: My First MongoDB Application
Back to Basics: My First MongoDB ApplicationBack to Basics: My First MongoDB Application
Back to Basics: My First MongoDB Application
MongoDB
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation FrameworkTyler Brock
 
Back to Basics Webinar 3: Introduction to Replica Sets
Back to Basics Webinar 3: Introduction to Replica SetsBack to Basics Webinar 3: Introduction to Replica Sets
Back to Basics Webinar 3: Introduction to Replica Sets
MongoDB
 
MongoDB World 2016 : Advanced Aggregation
MongoDB World 2016 : Advanced AggregationMongoDB World 2016 : Advanced Aggregation
MongoDB World 2016 : Advanced Aggregation
Joe Drumgoole
 
Java development with MongoDB
Java development with MongoDBJava development with MongoDB
Java development with MongoDB
James Williams
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
antoinegirbal
 

What's hot (20)

Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB Application
 
MongoDB - Aggregation Pipeline
MongoDB - Aggregation PipelineMongoDB - Aggregation Pipeline
MongoDB - Aggregation Pipeline
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
 
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
 
Back to Basics Webinar 5: Introduction to the Aggregation Framework
Back to Basics Webinar 5: Introduction to the Aggregation FrameworkBack to Basics Webinar 5: Introduction to the Aggregation Framework
Back to Basics Webinar 5: Introduction to the Aggregation Framework
 
Aggregation Framework in MongoDB Overview Part-1
Aggregation Framework in MongoDB Overview Part-1Aggregation Framework in MongoDB Overview Part-1
Aggregation Framework in MongoDB Overview Part-1
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
 
The Aggregation Framework
The Aggregation FrameworkThe Aggregation Framework
The Aggregation Framework
 
Getting Started with MongoDB and NodeJS
Getting Started with MongoDB and NodeJSGetting Started with MongoDB and NodeJS
Getting Started with MongoDB and NodeJS
 
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop ConnectorAnalytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop Connector
 
Webinar: Data Processing and Aggregation Options
Webinar: Data Processing and Aggregation OptionsWebinar: Data Processing and Aggregation Options
Webinar: Data Processing and Aggregation Options
 
Aggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days MunichAggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days Munich
 
Aggregation in MongoDB
Aggregation in MongoDBAggregation in MongoDB
Aggregation in MongoDB
 
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
MongoDB: Comparing WiredTiger In-Memory Engine to RedisMongoDB: Comparing WiredTiger In-Memory Engine to Redis
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
 
Back to Basics: My First MongoDB Application
Back to Basics: My First MongoDB ApplicationBack to Basics: My First MongoDB Application
Back to Basics: My First MongoDB Application
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
 
Back to Basics Webinar 3: Introduction to Replica Sets
Back to Basics Webinar 3: Introduction to Replica SetsBack to Basics Webinar 3: Introduction to Replica Sets
Back to Basics Webinar 3: Introduction to Replica Sets
 
MongoDB World 2016 : Advanced Aggregation
MongoDB World 2016 : Advanced AggregationMongoDB World 2016 : Advanced Aggregation
MongoDB World 2016 : Advanced Aggregation
 
Java development with MongoDB
Java development with MongoDBJava development with MongoDB
Java development with MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 

Viewers also liked

A Translational Medicine Platform at Sanofi
A Translational Medicine Platform at SanofiA Translational Medicine Platform at Sanofi
A Translational Medicine Platform at SanofiMongoDB
 
MongoDB Days UK: Using MongoDB and Python for Data Analysis Pipelines
MongoDB Days UK: Using MongoDB and Python for Data Analysis PipelinesMongoDB Days UK: Using MongoDB and Python for Data Analysis Pipelines
MongoDB Days UK: Using MongoDB and Python for Data Analysis Pipelines
MongoDB
 
MongoDB Certification Study Group - May 2016
MongoDB Certification Study Group - May 2016MongoDB Certification Study Group - May 2016
MongoDB Certification Study Group - May 2016
Norberto Leite
 
Data Engineering 101: Building your first data product by Jonathan Dinu PyDat...
Data Engineering 101: Building your first data product by Jonathan Dinu PyDat...Data Engineering 101: Building your first data product by Jonathan Dinu PyDat...
Data Engineering 101: Building your first data product by Jonathan Dinu PyDat...
PyData
 
Relationship Extraction from Unstructured Text-Based on Stanford NLP with Spa...
Relationship Extraction from Unstructured Text-Based on Stanford NLP with Spa...Relationship Extraction from Unstructured Text-Based on Stanford NLP with Spa...
Relationship Extraction from Unstructured Text-Based on Stanford NLP with Spa...
Spark Summit
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
Matt Harrison
 

Viewers also liked (6)

A Translational Medicine Platform at Sanofi
A Translational Medicine Platform at SanofiA Translational Medicine Platform at Sanofi
A Translational Medicine Platform at Sanofi
 
MongoDB Days UK: Using MongoDB and Python for Data Analysis Pipelines
MongoDB Days UK: Using MongoDB and Python for Data Analysis PipelinesMongoDB Days UK: Using MongoDB and Python for Data Analysis Pipelines
MongoDB Days UK: Using MongoDB and Python for Data Analysis Pipelines
 
MongoDB Certification Study Group - May 2016
MongoDB Certification Study Group - May 2016MongoDB Certification Study Group - May 2016
MongoDB Certification Study Group - May 2016
 
Data Engineering 101: Building your first data product by Jonathan Dinu PyDat...
Data Engineering 101: Building your first data product by Jonathan Dinu PyDat...Data Engineering 101: Building your first data product by Jonathan Dinu PyDat...
Data Engineering 101: Building your first data product by Jonathan Dinu PyDat...
 
Relationship Extraction from Unstructured Text-Based on Stanford NLP with Spa...
Relationship Extraction from Unstructured Text-Based on Stanford NLP with Spa...Relationship Extraction from Unstructured Text-Based on Stanford NLP with Spa...
Relationship Extraction from Unstructured Text-Based on Stanford NLP with Spa...
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 

Similar to Using MongoDB and Python

Mongodb intro
Mongodb introMongodb intro
Mongodb intro
christkv
 
MongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overview
Antonio Pintus
 
MongoDB_ppt.pptx
MongoDB_ppt.pptxMongoDB_ppt.pptx
MongoDB_ppt.pptx
1AP18CS037ShirishKul
 
Einführung in MongoDB
Einführung in MongoDBEinführung in MongoDB
Einführung in MongoDB
NETUserGroupBern
 
MongoDB at FrozenRails
MongoDB at FrozenRailsMongoDB at FrozenRails
MongoDB at FrozenRails
Mike Dirolf
 
Barcelona MUG MongoDB + Hadoop Presentation
Barcelona MUG MongoDB + Hadoop PresentationBarcelona MUG MongoDB + Hadoop Presentation
Barcelona MUG MongoDB + Hadoop PresentationNorberto Leite
 
MongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behlMongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behl
TO THE NEW | Technology
 
10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCup10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCup
WebGeek Philippines
 
MongoDB Internals
MongoDB InternalsMongoDB Internals
MongoDB Internals
Siraj Memon
 
Using MongoDB For BigData in 20 Minutes
Using MongoDB For BigData in 20 MinutesUsing MongoDB For BigData in 20 Minutes
Using MongoDB For BigData in 20 Minutes
András Fehér
 
Webinar: Was ist neu in MongoDB 2.4
Webinar: Was ist neu in MongoDB 2.4Webinar: Was ist neu in MongoDB 2.4
Webinar: Was ist neu in MongoDB 2.4
MongoDB
 
Mongo Bb - NoSQL tutorial
Mongo Bb - NoSQL tutorialMongo Bb - NoSQL tutorial
Mongo Bb - NoSQL tutorial
Mohan Rathour
 
MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)
Uwe Printz
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
Raghunath A
 
DBVersity MongoDB Online Training Presentations
DBVersity MongoDB Online Training PresentationsDBVersity MongoDB Online Training Presentations
DBVersity MongoDB Online Training Presentations
Srinivas Mutyala
 
Fast querying indexing for performance (4)
Fast querying   indexing for performance (4)Fast querying   indexing for performance (4)
Fast querying indexing for performance (4)
MongoDB
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You Scale
MongoDB
 
How to use MongoDB with CakePHP
How to use MongoDB with CakePHPHow to use MongoDB with CakePHP
How to use MongoDB with CakePHP
ichikaway
 
Couchbas for dummies
Couchbas for dummiesCouchbas for dummies
Couchbas for dummies
Qureshi Tehmina
 
Introduction To MongoDB
Introduction To MongoDBIntroduction To MongoDB
Introduction To MongoDB
ElieHannouch
 

Similar to Using MongoDB and Python (20)

Mongodb intro
Mongodb introMongodb intro
Mongodb intro
 
MongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overview
 
MongoDB_ppt.pptx
MongoDB_ppt.pptxMongoDB_ppt.pptx
MongoDB_ppt.pptx
 
Einführung in MongoDB
Einführung in MongoDBEinführung in MongoDB
Einführung in MongoDB
 
MongoDB at FrozenRails
MongoDB at FrozenRailsMongoDB at FrozenRails
MongoDB at FrozenRails
 
Barcelona MUG MongoDB + Hadoop Presentation
Barcelona MUG MongoDB + Hadoop PresentationBarcelona MUG MongoDB + Hadoop Presentation
Barcelona MUG MongoDB + Hadoop Presentation
 
MongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behlMongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behl
 
10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCup10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCup
 
MongoDB Internals
MongoDB InternalsMongoDB Internals
MongoDB Internals
 
Using MongoDB For BigData in 20 Minutes
Using MongoDB For BigData in 20 MinutesUsing MongoDB For BigData in 20 Minutes
Using MongoDB For BigData in 20 Minutes
 
Webinar: Was ist neu in MongoDB 2.4
Webinar: Was ist neu in MongoDB 2.4Webinar: Was ist neu in MongoDB 2.4
Webinar: Was ist neu in MongoDB 2.4
 
Mongo Bb - NoSQL tutorial
Mongo Bb - NoSQL tutorialMongo Bb - NoSQL tutorial
Mongo Bb - NoSQL tutorial
 
MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
DBVersity MongoDB Online Training Presentations
DBVersity MongoDB Online Training PresentationsDBVersity MongoDB Online Training Presentations
DBVersity MongoDB Online Training Presentations
 
Fast querying indexing for performance (4)
Fast querying   indexing for performance (4)Fast querying   indexing for performance (4)
Fast querying indexing for performance (4)
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You Scale
 
How to use MongoDB with CakePHP
How to use MongoDB with CakePHPHow to use MongoDB with CakePHP
How to use MongoDB with CakePHP
 
Couchbas for dummies
Couchbas for dummiesCouchbas for dummies
Couchbas for dummies
 
Introduction To MongoDB
Introduction To MongoDBIntroduction To MongoDB
Introduction To MongoDB
 

Recently uploaded

一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
balafet
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
NABLAS株式会社
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
pchutichetpong
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
AbhimanyuSinha9
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 

Recently uploaded (20)

一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 

Using MongoDB and Python

  • 1. Using MongoDB and Python Michael Bright Python User Group, Grenoble 23 Fevrier 2016
  • 3. Databases Relational Databases (RDBMS) “Les SGBDRs” (OLTP – Online Transaction Processing) • Based on relational database model invented by E.F. Codd (IBM) in 1970 • SQL (SEQUEL): expressive query language • Achieves performance increase through “vertical scaling” (higher performance node) NoSQL (No SQL, or “Not just SQL”?) existed before RDBMS, resurged out of a need for more Scalability for Web2.0 - Columnar, e.g. Cassandra, Hbase, Vertica - Document Oriented, e.g. CouchDB, MongoDB, RethinkDB - Graph, e.g. Neo4J - Key-value, e.g. CouchDB, Dynamo, Riak, Redis, Oracle NoSQL, MUMPS - Multi-model, e.g. Alchemy, ArangoDB • Achieves performance increase through “horizontal scaling” (by adding nodes) OLAP – Online Analytic Processing
  • 4. Databases for scalability The need for scalability requires distribution of processing making it more difficult to satisfy all criteria, according to the CAP Theorem (2000) - Consistency (Atomicity) - Availability (A request will always receive a response) - Partition Tolerance (if nodes become disconnected, the system continues to function) It is impossible to satisfy all three constraints (proved by MIT, 2002). RDBMS - favour Consistency and Availability but cannot scale horizontally NoSQL - favour Consistency and Partitioning or Availability and Partitioning
  • 5. Relational vs. NoSQL Relational databases provide - Rich query language (SQL) - Strong consistency - Secondary indices NoSQL provides - Flexibility - “dynamic schema” allows missing or extra fields, embedded structures - Scalability - Adding nodes allows to scale data size - Performance - Adding nodes allows to scale performance MongoDB combines both sets of advantages
  • 7. MongoDB – a Document-Oriented DB Open source DB on github with commercial support from MongoDB Inc (was 10gen). • Dynamic schema (schema-less) aids agile development • A document is an associative array (possibly more than 1-level deep), e.g. JSON Object • Uses BSON (binary JSON format) for serialization (easy to parse) • Binaries downloadable for Linux(es), Windows, OSX, Solaris • Drivers available in many languages • Provides an aggregation framework • Is the “M” in MEAN
  • 8. MongoDB – Performance • Written in C++ • It achieves high performance, especially on dynamic queries • Allows primary & secondary indices (main tuning element) • Is horizontally scalable across many nodes • Extensive use of memory-mapped files i.e. read-through write-through memory caching. • Provides high availability with Master/slave replication • Provides Sharding (horizontal partitioning of data across nodes) • Horizontal partitioning : complete documents (or rows) are stored on a node • Vertical partitioning: documents (or rows) are split across nodes
  • 10. MongoDB – Terminology RDBMS MongoDB Table, View ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard
  • 11. MongoDB – Enterprise Products There are supported products for the enterprise • MongoDB Enterprise: for production workloads • MongoDB Compass: Data and Schema visualization tool • MongoDB Connector for BI: Allows users to visualize their MongoDB Enterprise data using existing relational business intelligence tools such as Tableau. Foreign data wrapper with PostgreSQL to provide a relational SQL view on MongoDB data
  • 12. MongoDB v3.2: new features • The default storage engine is now WiredTiger • Following acquisition of WiredTiger, Inc. • Introduced as an optional engine in MongoDB 3.0 • Has more granular concurrency control and native compression (lowering storage costs, increasing h/w utilization, throughput and providing more predictable performance) • Replication election enhancements • Config servers as replica sets • readConcern, and document validations. • OpsManager 2.0
  • 13. MongoDB v3.2: new aggregation features • $sample, $lookup • $indexStats • $filter,$slice,$arrayElemAt, $isArray, $concatArrays • Partial Indexes • Document Validation • 4 bit testing operators • 2 new Accumulators for $group • 10 new Arithmetic expression operators
  • 14. MongoDB components Components mongod - The database daemon process. mongos - Sharding controller. mongo - The database shell (uses interactive javascript). Utilities mongodump - MongoDB dump tool - for backups, snapshots, etc. mongorestore - MongoDB restore a dump mongoexport - Export a single collection to test (JSON, CSV) mongoimport - Import from JSON or CSV mongofiles - Utility for putting and getting files from MongoDB GridFS mongostat - Show performance statistics
  • 16. MongoDB drivers exist for many languages 12 official drivers, plus many community languages
  • 19. MongoDB shell > mongo MongoDB shell version: 3.2.3 connecting to: test > show dbs Money_UK 0.000GB aggregation_example 0.000GB local 0.000GB test 0.000GB > use Money_UK switched to db Money_UK > show collections CARD SAVING
  • 20. MongoDB shell > use test switched to db test > db.newcoll.insert({ 'field1': 'an example' }) WriteResult({ "nInserted" : 1 }) > db.newcoll.find().count() 1 > db.newcoll.find() { "_id" : ObjectId("56cc7446bb127b86163cf226"), "field1" : "an example" } > db.newcoll.findOne() { "_id" : ObjectId("56cc7446bb127b86163cf226"), "field1" : "an example" } > db.newcoll.find({'_id':ObjectId("56cc7446bb127b86163cf226")}) { "_id" : ObjectId("56cc7446bb127b86163cf226"), "field1" : "an example" }
  • 21. MongoDB shell > var doc2={'field1': 'example', 'field2': 'example2'} > db.newcoll.insert(doc2) WriteResult({ "nInserted" : 1 }) > db.newcoll.find().count() 2 > db.newcoll.find().pretty() { "_id" : ObjectId("56cc7446bb127b86163cf226"), "field1" : "an example" } { "_id" : ObjectId("56cc7711bb127b86163cf227"), "field1" : "example", "field2" : "example2" }
  • 22. MongoDB indexing Indexing is the single biggest tunable performance factor. Can index on a single field, e.g. on ascending or descending values: db.newcoll.ensureIndex( { ‘field1’: 1 } ) // or -1: descending Or subfields: db.newcoll.ensureIndex( { ‘arr.subfield’: 1 } ) Or multiple fields: db.newcoll.ensureIndex( { ‘arr.subfield’: 1, ‘field2’: -1 } )
  • 23. MongoDB indexing - 2 It is also possible to provide hints to the indexer, on uniqueness, sparseness or to request index creation as a background task, e.g. db.newcoll.ensureIndex( { ‘field1’: 1 }, { ‘unique’: true} ) db.newcoll.ensureIndex( { ‘field1’: 1 , ‘field2’: 1}, { ‘sparse’: true} ) db.newcoll.ensureIndex( { ‘field1’: 1 }, { ‘background’: true} )
  • 24. MongoDB searching We can search based on a single field db.newcoll.find( { ‘field1’: ‘any match’ } ) Or subfields: db.newcoll.find( { ‘arr.subfield’: ‘sub field match’ } ) Or multiple fields: db.newcoll.find( { ‘arr.subfield’: ‘sub field match’, ‘field2’: ‘match2’ } ) Available indices will be used if possible
  • 25. MongoDB sorting We can sort the results in ascending or descending order db.newcoll.find().sort( { ‘field1’: 1 } ) // or -1: descending or db.newcoll.find().sort( { ‘field1’: 1, ‘field2’: -1 } ) Available indices will be used if possible
  • 26. MongoDB projections We can reduce the number of returned fields (the projection). This may enable data access directly from the index. In this example we limit the returned fields to ‘field1’ (and the ‘_id’ index). db.newcoll.find({ ‘field1’: ‘example’ }, { ‘field1’: 1} ) In this example we limit the returned fields to ‘field1’ (without the ‘_id’ index). db.newcoll.find({ ‘field1’: ‘example’ }, { ‘_id’: 0, ‘field1’: 1} ) We can also override the default index db.newcoll.find({ ‘field1’: ‘example’ }, { ‘_id’: ‘field1’ } )
  • 27. MongoDB explain plan We can request explanation of how a query will be handled (showing possible index use) > db.newcoll.find().explain() { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.newcoll", "indexFilterSet" : false, "parsedQuery" : { "$and" : [ ] }, "winningPlan" : { "stage" : "COLLSCAN", "filter" : { "$and" : [ ] }, "direction" : "forward" }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "MJBRIGHT7", "port" : 27017, "version" : "3.2.3", "gitVersion" : "b326ba837cf6f49d65c2f85e1b70f6f31ece7937" }, "ok" : 1 }
  • 28. MongoDB special index types • Geospatial Indexes (2d Sphere) • Text Indexes • TTL Collections (expireAfterSeconds) • Hashed Indexes for sharding
  • 29. MongoDB aggregation framework Aggregation uses a pipeline of operations amongst • $match • $project • $unwind • $group • $sort
  • 30. MongoDB aggregation example pipeline = [ { "$project": { // Select fields of interest 'year': { "$dateToString": { 'format': "%Y", 'date': "$date" } }, 'tags': 1, 'value': 1, }, }, { "$match": { 'year': str(year) }}, { "$group": {"_id": "$tags", "total": {"$sum": {"$abs": "$value"}}} }, { "$sort": SON([("total", -1), ("_id", -1)]) } ] cursor = db.collection.aggregate(pipeline)
  • 31. MongoDB aggregation example pipeline = [ { "$project": { 'year': { "$dateToString": { 'format': "%Y", 'date': "$date" } }, 'tags': 1, 'value': 1, }, }, { "$match": { 'year': str(year) }}, // match on fields { "$group": {"_id": "$tags", "total": {"$sum": {"$abs": "$value"}}} }, { "$sort": SON([("total", -1), ("_id", -1)]) } ] cursor = db.collection.aggregate(pipeline)
  • 32. MongoDB aggregation example pipeline = [ { "$project": { 'year': { "$dateToString": { 'format': "%Y", 'date': "$date" } }, 'tags': 1, 'value': 1, }, }, { "$match": { 'year': str(year) }}, { "$group": {"_id": "$tags", "total": {"$sum": {"$abs": "$value"}}} }, // ‘reduce’ { "$sort": SON([("total", -1), ("_id", -1)]) } ] cursor = db.collection.aggregate(pipeline)
  • 33. MongoDB aggregation example pipeline = [ { "$project": { 'year': { "$dateToString": { 'format': "%Y", 'date': "$date" } }, 'tags': 1, 'value': 1, }, }, { "$match": { 'year': str(year) }}, { "$group": {"_id": "$tags", "total": {"$sum": {"$abs": "$value"}}} }, { "$sort": SON([("total", -1), ("_id", -1)]) } // sort the results ] cursor = db.collection.aggregate(pipeline)
  • 34. PyMongo – ca y est, on arrive ! But first …
  • 35. PyMongo – ca y est, on arrive ! … there are alternatives to the official PyMongo driver. • Motor is a full-featured, non-blocking MongoDB driver for Python Tornado applications. • TxMongo is an asynchronous Twisted Python driver for MongoDB.
  • 36. PyMongo – ca y est, on arrive ! … and other Python/MongoDB tools ORM-like Layers New users are recommended to begin with PyMongo. Nevertheless other implementations are available providing higher abstraction. Humongolus, MongoKit, Ming, MongoAlchemy, MongoEngine, Minimongo, Manga, MotorEngine Framework Tools This section lists tools and adapters that have been designed to work with various Python frameworks and libraries. Django MongoDB Engine, Mango, Django MongoEngine, mongodb_beaker, Log4Mongo, MongoLog, C5t, rod.recipe.mongodb, repoze-what-plugins-mongodb, Mongobox, Flask- MongoAlchemy, Flask-MongoKit, Flask-PyMongo.