Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RESTful Web API and MongoDB go for a pic nic

RESTful Web API and MongoDB go for a pic nic

Why and how MongoDB is a perfect match for building your next RESTful Web API.

Nicola Iarocci

October 26, 2013
Tweet

More Decks by Nicola Iarocci

Other Decks in Programming

Transcript

  1. Nicola Iarocci Eve REST API Framework, Cerberus, Events Co-founder @

    gestionaleamica.com, Book Author, MongoDB Master
  2. Il Piccolo  Libro di MongoDB edizione italiana del libro

    di Karl Seguin disponibile per il download @ nicolaiarocci.com
  3. Constraints • minimum viable product first • add features over

    time • frequent database schema updates • avoid downtime as much as possible
  4. JSON & RESTful API JSON! accepted media type Client JSON!

    (BSON) Mongo GET maybe we can push directly to client?
  5. JSON & RESTful API JSON! accepted media type Client JSON!

    (BSON) Mongo JSON! subset of python dict! (kinda) API GET almost.
  6. JSON & RESTful API JSON! objects Client JSON! (BSON) Mongo

    JSON/dict! maps to python dict! (validation layer) API POST also works when posting (adding) items to the database
  7. Queries in MongoDB are represented as JSON-style objects What about

    Queries? // select * from things where x=3 and y="foo" db.things.find({x: 3, y: "foo”});
  8. Filtering and Sorting native! Mongo! query syntax Client JSON! (BSON)

    Mongo (very) thin parsing! & validation layer API let’s simply expose MongoDB syntax ?where={x: 3, y: "foo”}
  9. Also in MongoDB • setup is a breeze • lightweight

    • fast inserts, updates and queries • excellent documentation • great support by 10gen • great community
  10. def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for

    document in cursor:! documents.append(document)! return documents Resource GET find() accepts a python dict as query expression, and returns a cursor we can iterate /contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1}
  11. def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for

    document in cursor:! documents.append(document)! return documents Resource GET find() accepts a python dict as query expression, and returns a cursor we can iterate /contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1}
  12. def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for

    document in cursor:! documents.append(document)! return documents Resource GET find() accepts a python dict as query expression, and returns a cursor we can iterate /contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1}
  13. PATCHing mongo update() method commits updates to the database. def

    patch_document(collection, original):! (...)! # Perform the update! db(collection).update({"_Id": ObjectId(object_id)}, ! {"$set": updates})!
  14. PATCHing udpate() takes the unique Id of the document to

    update def patch_document(collection, original):! (...)! # Perform the update! db(collection).update({"_Id": ObjectId(object_id)}, ! {"$set": updates})!
  15. 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
  16. def post(collection):! (...)! for key, item in docs.items():! response[ID_FIELD] =

    db(collection).insert(item) POSTing Take #1 push document and get its ObjectId back from Mongo. like other CRUD operations, inserting is trivial in mongo.
  17. Beta 0.2 • 1.000+ stargazers • 120 forks • 24

    contributors • 7.935 downloads