SlideShare a Scribd company logo
PyMongo for the Clueless


      klrkdekira@gmail.com
http://www.python.org
http://www.mongodb.org/
● json-like (bson) document store

● full index support

● replication and high availability

● auto sharding

● querying

● fast in-place update

● map/reduce

● GridFS
SQL to Mongo Mapping

MySQL Term    MongoTerm

database      database

table         collection

index         index

row           BSON document

column        BSON field

join          embedding and linking

primary key   _id field
PyMongo

The romantic story of

          and
Connection Part 1
>>> import pymongo

>>> conn = pymongo.Connection()

is equivalent to

>>> conn = pymongo.Connection('localhost', 27017)

Getting the database

>>> db = conn.testdb

is equivalent to

>>> db = conn['testdb']
Basic Part 2
Getting the collection

>>> collection = db.test_collection

also is equivalent to

>>> collection = db['test_collection']

Make it understood from now
Insert New Document(s)
>>> dummy = db['dummy']

>>> dummy.insert({'key': 'a',
                 'value': 1})
ObjectId('4e7c50eb059bf61d2f000000')

Multiple insert

>>> docs = [{'key':'b', 'value':2},
           {'key':'c', 'value':3}]

>>> dummy.insert(docs)
[ObjectId('4e7c552a059bf61d2f000002'), ObjectId
('4e7c552a059bf61d2f000003')]
Find Part 1
>>> dummy.find_one()
{u'_id': ObjectId('4e7c50eb059bf61d2f000000'), u'key': u'a', u'value': 1}

>>> dummy.find()
<pymongo.cursor.Cursor object at 0x2810ed0>

>>> for d in dummy.find():
       print d
{u'_id': ObjectId('4e7c50eb059bf61d2f000000'), u'key': u'a', u'value': 1}
{u'_id': ObjectId('4e7c5544059bf61d2f000004'), u'key': u'b',
u'value': 2}
{u'_id': ObjectId('4e7c5544059bf61d2f000005'), u'key': u'c',
u'value': 3}
Find Part 2
>>> dummy.find_one({'key':'a'})
{u'_id': ObjectId('4e7c50eb059bf61d2f000000'), u'key': u'a', u'value': 1}

Query by ObjectId

>>> from bson.objectid import ObjectId

>>> dummy.find_one({'_id':ObjectId('4e7c50eb059bf61d2f000000')})
{u'_id': ObjectId('4e7c50eb059bf61d2f000000'), u'key': u'a', u'value': 1}

Same goes with find.
Update
>>> dummy.update({'key':'a'},
               {'value':2})

Update multiple document, destructive

>>> dummy.update({'key':'a'},
               {'value':2},
               multi=True)

Just to overwrite attributes

>>> dummy.update({'key':'a'},
               {'$set':{'value':2}})
Delete
Remove multiple file

>>> dummy.remove({'key':'a'})

Remove by ObjectId

>>> dummy.remove({'_id':ObjectId('4e7c50eb059bf61d2f000000')})
Stored JS
>>> db.system.js.add = "function (x, y) { return x + y; }"

>>> db.system.js.add(1, 2)
3.0

>>> del db.system.js.add
QUESTION   ?
Pymongo for the Clueless

More Related Content

PDF
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
TXT
Litebox
PPTX
Super Advanced Python –act1
PPTX
Lab 13
PPTX
Everyday's JS
PDF
MongoDB Oplog入門
PDF
Ooprc4 b
PDF
20160616技術會議
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Litebox
Super Advanced Python –act1
Lab 13
Everyday's JS
MongoDB Oplog入門
Ooprc4 b
20160616技術會議

What's hot (20)

PDF
Pemrograman visual
PDF
Clojure functions midje
PDF
MongoDBで作るソーシャルデータ新解析基盤
PDF
The Ring programming language version 1.3 book - Part 43 of 88
PDF
MongoDB全機能解説2
DOCX
Punto fijo multivariante
PDF
C++ practical
PDF
#RuPostgresLive 4: как писать и читать сложные SQL-запросы
PDF
The Ring programming language version 1.9 book - Part 77 of 210
PDF
First step of Performance Tuning
PPT
Indexing & query optimization
PPTX
Mongo db modifiers
PDF
Tugas struktur data terakhir_pohonBiner
DOC
Atm machine using c++
DOC
Atm machine using c++
PDF
Oopsprc1c
DOC
Atm machine using c++
DOC
Atm machine using c++
PDF
はじめてのGroovy
PDF
RIA - Entwicklung mit Ext JS
Pemrograman visual
Clojure functions midje
MongoDBで作るソーシャルデータ新解析基盤
The Ring programming language version 1.3 book - Part 43 of 88
MongoDB全機能解説2
Punto fijo multivariante
C++ practical
#RuPostgresLive 4: как писать и читать сложные SQL-запросы
The Ring programming language version 1.9 book - Part 77 of 210
First step of Performance Tuning
Indexing & query optimization
Mongo db modifiers
Tugas struktur data terakhir_pohonBiner
Atm machine using c++
Atm machine using c++
Oopsprc1c
Atm machine using c++
Atm machine using c++
はじめてのGroovy
RIA - Entwicklung mit Ext JS
Ad

Similar to Pymongo for the Clueless (20)

PPTX
Mongodatabase with Python for Students.pptx
PPTX
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
PPTX
Python With MongoDB in advanced Python.pptx
PDF
MongoDB and Python
PDF
Python and MongoDB
KEY
Round pegs and square holes
PPT
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
PPS
MongoDB crud
PPTX
lecture_34e.pptx
PPTX
Python mongo db-training-europython-2011
PPTX
No SQL DB lecture showing structure and syntax
ODP
PPTX
Mongo db basic installation
PPTX
Introduction to MongoDB
PPTX
MongoDB (Advanced)
PPTX
Mongo Nosql CRUD Operations
KEY
Inside PyMongo - MongoNYC
PPT
Mongo db tutorials
PDF
Mongo db basics
PDF
Mongo learning series
Mongodatabase with Python for Students.pptx
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Python With MongoDB in advanced Python.pptx
MongoDB and Python
Python and MongoDB
Round pegs and square holes
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
MongoDB crud
lecture_34e.pptx
Python mongo db-training-europython-2011
No SQL DB lecture showing structure and syntax
Mongo db basic installation
Introduction to MongoDB
MongoDB (Advanced)
Mongo Nosql CRUD Operations
Inside PyMongo - MongoNYC
Mongo db tutorials
Mongo db basics
Mongo learning series
Ad

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
KodekX | Application Modernization Development
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation theory and applications.pdf
20250228 LYD VKU AI Blended-Learning.pptx
KodekX | Application Modernization Development
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation_ Review paper, used for researhc scholars
MYSQL Presentation for SQL database connectivity
Network Security Unit 5.pdf for BCA BBA.
MIND Revenue Release Quarter 2 2025 Press Release
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
sap open course for s4hana steps from ECC to s4
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
cuic standard and advanced reporting.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing

Pymongo for the Clueless

  • 1. PyMongo for the Clueless klrkdekira@gmail.com
  • 4. ● json-like (bson) document store ● full index support ● replication and high availability ● auto sharding ● querying ● fast in-place update ● map/reduce ● GridFS
  • 5. SQL to Mongo Mapping MySQL Term MongoTerm database database table collection index index row BSON document column BSON field join embedding and linking primary key _id field
  • 7. Connection Part 1 >>> import pymongo >>> conn = pymongo.Connection() is equivalent to >>> conn = pymongo.Connection('localhost', 27017) Getting the database >>> db = conn.testdb is equivalent to >>> db = conn['testdb']
  • 8. Basic Part 2 Getting the collection >>> collection = db.test_collection also is equivalent to >>> collection = db['test_collection'] Make it understood from now
  • 9. Insert New Document(s) >>> dummy = db['dummy'] >>> dummy.insert({'key': 'a', 'value': 1}) ObjectId('4e7c50eb059bf61d2f000000') Multiple insert >>> docs = [{'key':'b', 'value':2}, {'key':'c', 'value':3}] >>> dummy.insert(docs) [ObjectId('4e7c552a059bf61d2f000002'), ObjectId ('4e7c552a059bf61d2f000003')]
  • 10. Find Part 1 >>> dummy.find_one() {u'_id': ObjectId('4e7c50eb059bf61d2f000000'), u'key': u'a', u'value': 1} >>> dummy.find() <pymongo.cursor.Cursor object at 0x2810ed0> >>> for d in dummy.find(): print d {u'_id': ObjectId('4e7c50eb059bf61d2f000000'), u'key': u'a', u'value': 1} {u'_id': ObjectId('4e7c5544059bf61d2f000004'), u'key': u'b', u'value': 2} {u'_id': ObjectId('4e7c5544059bf61d2f000005'), u'key': u'c', u'value': 3}
  • 11. Find Part 2 >>> dummy.find_one({'key':'a'}) {u'_id': ObjectId('4e7c50eb059bf61d2f000000'), u'key': u'a', u'value': 1} Query by ObjectId >>> from bson.objectid import ObjectId >>> dummy.find_one({'_id':ObjectId('4e7c50eb059bf61d2f000000')}) {u'_id': ObjectId('4e7c50eb059bf61d2f000000'), u'key': u'a', u'value': 1} Same goes with find.
  • 12. Update >>> dummy.update({'key':'a'}, {'value':2}) Update multiple document, destructive >>> dummy.update({'key':'a'}, {'value':2}, multi=True) Just to overwrite attributes >>> dummy.update({'key':'a'}, {'$set':{'value':2}})
  • 13. Delete Remove multiple file >>> dummy.remove({'key':'a'}) Remove by ObjectId >>> dummy.remove({'_id':ObjectId('4e7c50eb059bf61d2f000000')})
  • 14. Stored JS >>> db.system.js.add = "function (x, y) { return x + y; }" >>> db.system.js.add(1, 2) 3.0 >>> del db.system.js.add
  • 15. QUESTION ?