KEMBAR78
REST Web API with MongoDB | PDF
RESTful Web API
     with MongoDB




    Nicola Iarocci
Good Morning.
Italian edition




      Il Piccolo
  Libro di MongoDB
                     by Karl Seguin, traduzione di
                         Nicola Iarocci
http://nicolaiarocci.com/il-piccolo-libro-di-mongodb-edizione-italiana/
gestionaleamica.com
    invoicing & accounting
Your Typical Old School
    Desktop app...




... now going web & mobile
MongoDB
and REST
           or
why we picked MongoDB
   for our REST API
Similarity with
              RDBMS
made NoSQL easy to grasp (even for a dumbhead like me)
Terminology
RDBMS           Mongo
Database        Database

 Table         Collection

Rows(s)     JSON Document

 Index           Index

  Join     Embedding & Linking
JSON-style data store
     true selling point for me
JSON & RESTful API
                            GET


    Client                                     Mongo

     JSON                                       JSON
accepted media type                                 (BSON)




            maybe we can push directly to client?
JSON & RESTful API
                           GET


    Client                API               Mongo

     JSON              JSON/dict            JSON
accepted media type   maps to python dict   (BSON)




                          almost.
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
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”});
JSON & RESTful API
                FILTERING & SORTING


               ?where={x: 3, y: "foo”}


Client                  API              Mongo
                      (very) thin
native                  parsing          JSON
Mongo                 & validation       (BSON)
query syntax
                         layer
JSON
 all along the pipeline
mapping to and from the database feels more natural
Schema-less
dynamic objects allow for a painless evolution of our schema
    (because yes, a schema exists at any point in time)
ORM
Where we’re going we don’t need ORMs.
PyMongo
   official Python driver
MongoDB drivers are beautiful
Also in MongoDB

• setup is a breeze
• lightweight
• fast inserts, updates and queries
• excellent documentation
• great support by 10gen
• great community
Collections
 API’s entry point + plural nouns
http://api.example.com/v1/contacts
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
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
PATCH
Editing a Resource
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.
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
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
POST
Creating Resources
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.
Introducing
     Eve
Open Source RESTful Web API
   powered by MongoDB
in !
   rk ess
wo gr
 p ro




     he r y
  t t to
ge ll s !
  u ere
 f h


              ... stay tuned!
Wanna see it running?
 Hopefully it won’t explode right into my face
Thank you.
 english: @nicolaiarocci
italian: nicolaiarocci.com

REST Web API with MongoDB

  • 1.
    RESTful Web API with MongoDB Nicola Iarocci
  • 2.
  • 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/
  • 4.
    gestionaleamica.com invoicing & accounting
  • 5.
    Your Typical OldSchool Desktop app... ... now going web & mobile
  • 6.
    MongoDB and REST or why we picked MongoDB for our REST API
  • 7.
    Similarity with RDBMS made NoSQL easy to grasp (even for a dumbhead like me)
  • 8.
    Terminology RDBMS Mongo Database Database Table Collection Rows(s) JSON Document Index Index Join Embedding & Linking
  • 9.
    JSON-style data store true selling point for me
  • 10.
    JSON & RESTfulAPI GET Client Mongo JSON JSON accepted media type (BSON) maybe we can push directly to client?
  • 11.
    JSON & RESTfulAPI GET Client API Mongo JSON JSON/dict JSON accepted media type maps to python dict (BSON) almost.
  • 12.
    JSON & RESTfulAPI 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? Queriesin MongoDB are represented as JSON-style objects // select * from things where x=3 and y="foo" db.things.find({x: 3, y: "foo”});
  • 14.
    JSON & RESTfulAPI FILTERING & SORTING ?where={x: 3, y: "foo”} Client API Mongo (very) thin native parsing JSON Mongo & validation (BSON) query syntax layer
  • 15.
    JSON all alongthe pipeline mapping to and from the database feels more natural
  • 16.
    Schema-less dynamic objects allowfor a painless evolution of our schema (because yes, a schema exists at any point in time)
  • 17.
    ORM Where we’re goingwe don’t need ORMs.
  • 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
  • 20.
    Collections API’s entrypoint + plural nouns http://api.example.com/v1/contacts
  • 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
  • 23.
  • 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
  • 27.
  • 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 itrunning? Hopefully it won’t explode right into my face
  • 32.
    Thank you. english:@nicolaiarocci italian: nicolaiarocci.com