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.

E3550767c858c787c35c280047ff789c?s=128

Nicola Iarocci

October 26, 2013
Tweet

Transcript

  1. 2.

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

    gestionaleamica.com, Book Author, MongoDB Master
  2. 4.

    Il Piccolo  Libro di MongoDB edizione italiana del libro

    di Karl Seguin disponibile per il download @ nicolaiarocci.com
  3. 7.
  4. 11.
  5. 12.

    Constraints • minimum viable product first • add features over

    time • frequent database schema updates • avoid downtime as much as possible
  6. 17.

    JSON & RESTful API JSON! accepted media type Client JSON!

    (BSON) Mongo GET maybe we can push directly to client?
  7. 18.

    JSON & RESTful API JSON! accepted media type Client JSON!

    (BSON) Mongo JSON! subset of python dict! (kinda) API GET almost.
  8. 19.

    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
  9. 22.

    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”});
  10. 23.

    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”}
  11. 28.

    Also in MongoDB • setup is a breeze • lightweight

    • fast inserts, updates and queries • excellent documentation • great support by 10gen • great community
  12. 33.

    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. 34.

    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}
  14. 35.

    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}
  15. 40.

    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})!
  16. 41.

    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})!
  17. 42.

    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
  18. 44.

    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.
  19. 48.

    Beta 0.2 • 1.000+ stargazers • 120 forks • 24

    contributors • 7.935 downloads
  20. 51.