3. Italian edition
Il Piccolo
Libro di MongoDB
by Karl Seguin, traduzione di
Nicola Iarocci
http://nicolaiarocci.com/il-piccolo-libro-di-mongodb-edizione-italiana/
10. JSON & RESTful API
GET
Client Mongo
JSON JSON
accepted media type (BSON)
maybe we can push directly to client?
11. JSON & RESTful API
GET
Client API Mongo
JSON JSON/dict JSON
accepted media type maps to python dict (BSON)
almost.
12. JSON & RESTful API
POST
Client API Mongo
JSON JSON/dict JSON
maps to python dict
objects (BSON)
(validation layer)
also works when posting (adding) items to the database
13. What about Queries?
Queries in MongoDB are represented as JSON-style objects
// select * from things where x=3 and y="foo"
db.things.find({x: 3, y: "foo”});
18. PyMongo
official Python driver
MongoDB drivers are beautiful
19. Also in MongoDB
• setup is a breeze
• lightweight
• fast inserts, updates and queries
• excellent documentation
• great support by 10gen
• great community
21. Document
Documents are identified by ObjectID
http://api.example.com/v1/contacts/4f46445fc88e201858000000
And eventually by an alternative lookup value
http://api.example.com/v1/contacts/CUST12345
22. Collection GET
http://api.example.com/v1/contacts?where={“age”: {“$gt”: 20}}
def get_collection(collection):
documents = []
cursor = db(collection).find(**args)
for document in cursor:
documents.append(document)
return documents
find() accepts a python dict
as query expression, and
returns a cursor we can
iterate
24. PATCHing
def patch_document(collection, original):
(...)
# Perform the update
db(collection).update({"_Id": ObjectId(object_id)},
{"$set": updates})
mongo update() method
commits updates to the
database.
25. PATCHing
def patch_document(collection, original):
(...)
# Perform the update
db(collection).update({"_Id": ObjectId(object_id)},
{"$set": updates})
udpate() takes the unique Id
of the document to update
26. PATCHing
def patch_document(collection, original):
(...)
# Perform the update
db(collection).update({"_Id": ObjectId(object_id)},
{"$set": updates})
$set accepts a dict
with the updates for the db
eg: {“active”: False}.
updates are atomic
28. POSTing
def post(collection):
(...)
for key, item in docs.items():
response[ID_FIELD] = db(collection).insert(item)
push document and get its
ObjectId back from Mongo.
like other CRUD operations,
inserting is trivial in
mongo.
29. Introducing
Eve
Open Source RESTful Web API
powered by MongoDB
30. in !
rk ess
wo gr
p ro
he r y
t t to
ge ll s !
u ere
f h
... stay tuned!
31. Wanna see it running?
Hopefully it won’t explode right into my face