REST Web API with MongoDB

8,265 views

Published on

From A Morning with MongoDB - Milan on October 24, 2012.

Published in: Technology

REST Web API with MongoDB

  1. 1. RESTful Web API with MongoDB Nicola Iarocci
  2. 2. Good Morning.
  3. 3. Italian edition Il Piccolo Libro di MongoDB by Karl Seguin, traduzione di Nicola Iaroccihttp://nicolaiarocci.com/il-piccolo-libro-di-mongodb-edizione-italiana/
  4. 4. gestionaleamica.com invoicing & accounting
  5. 5. Your Typical Old School Desktop app...... now going web & mobile
  6. 6. MongoDBand REST orwhy we picked MongoDB for our REST API
  7. 7. Similarity with RDBMSmade NoSQL easy to grasp (even for a dumbhead like me)
  8. 8. TerminologyRDBMS MongoDatabase Database Table CollectionRows(s) JSON Document Index Index Join Embedding & Linking
  9. 9. JSON-style data store true selling point for me
  10. 10. JSON & RESTful API GET Client Mongo JSON JSONaccepted media type (BSON) maybe we can push directly to client?
  11. 11. JSON & RESTful API GET Client API Mongo JSON JSON/dict JSONaccepted media type maps to python dict (BSON) almost.
  12. 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. 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”});
  14. 14. JSON & RESTful API FILTERING & SORTING ?where={x: 3, y: "foo”}Client API Mongo (very) thinnative parsing JSONMongo & validation (BSON)query syntax layer
  15. 15. JSON all along the pipelinemapping to and from the database feels more natural
  16. 16. Schema-lessdynamic objects allow for a painless evolution of our schema (because yes, a schema exists at any point in time)
  17. 17. ORMWhere we’re going we don’t need ORMs.
  18. 18. PyMongo official Python driverMongoDB drivers are beautiful
  19. 19. Also in MongoDB• setup is a breeze• lightweight• fast inserts, updates and queries• excellent documentation• great support by 10gen• great community
  20. 20. Collections API’s entry point + plural nounshttp://api.example.com/v1/contacts
  21. 21. Document Documents are identified by ObjectIDhttp://api.example.com/v1/contacts/4f46445fc88e201858000000 And eventually by an alternative lookup value http://api.example.com/v1/contacts/CUST12345
  22. 22. Collection GEThttp://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
  23. 23. PATCHEditing a Resource
  24. 24. PATCHingdef 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. 25. PATCHingdef 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. 26. PATCHingdef 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
  27. 27. POSTCreating Resources
  28. 28. POSTingdef 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. 29. Introducing EveOpen Source RESTful Web API powered by MongoDB
  30. 30. in ! rk esswo gr p ro he r y t t toge ll s ! u ere f h ... stay tuned!
  31. 31. Wanna see it running? Hopefully it won’t explode right into my face
  32. 32. Thank you. english: @nicolaiarocciitalian: nicolaiarocci.com

×