SlideShare a Scribd company logo
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

More Related Content

PPTX
Building Your First Application with MongoDB
PDF
RESTful Web API and MongoDB go for a pic nic
PPTX
Building Your First App with MongoDB
PDF
Eve - REST API for Humans™
PDF
Building Your First MongoDB App
KEY
MongoDB & Mongoid with Rails
PDF
Mongoid in the real world
PDF
MongoDB and Ruby on Rails
Building Your First Application with MongoDB
RESTful Web API and MongoDB go for a pic nic
Building Your First App with MongoDB
Eve - REST API for Humans™
Building Your First MongoDB App
MongoDB & Mongoid with Rails
Mongoid in the real world
MongoDB and Ruby on Rails

What's hot (20)

PPT
Advanced Json
PDF
Building your first app with MongoDB
KEY
Practical Ruby Projects With Mongo Db
PDF
Updates to the java api for json processing for java ee 8
PDF
Analyse Yourself
PDF
Differential Sync and JSON Patch @ SpringOne2GX 2014
PDF
JSON REST API for WordPress
PPTX
The JSON REST API for WordPress
PDF
Webinar: Getting Started with Ruby and MongoDB
PDF
Building Apps with MongoDB
PDF
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK - Nicola Iarocci - Co...
PPTX
Webinar: Building Your First App
PPTX
Introduction to JSON & AJAX
PDF
Introduction to MongoDB
PPTX
MongoDB + Java - Everything you need to know
PPTX
Introduction to MongoDB and Hadoop
PPTX
Crafting Evolvable Api Responses
PPTX
Using Webservice in iOS
PDF
Web services tutorial
PPTX
Mongo db queries
Advanced Json
Building your first app with MongoDB
Practical Ruby Projects With Mongo Db
Updates to the java api for json processing for java ee 8
Analyse Yourself
Differential Sync and JSON Patch @ SpringOne2GX 2014
JSON REST API for WordPress
The JSON REST API for WordPress
Webinar: Getting Started with Ruby and MongoDB
Building Apps with MongoDB
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK - Nicola Iarocci - Co...
Webinar: Building Your First App
Introduction to JSON & AJAX
Introduction to MongoDB
MongoDB + Java - Everything you need to know
Introduction to MongoDB and Hadoop
Crafting Evolvable Api Responses
Using Webservice in iOS
Web services tutorial
Mongo db queries
Ad

Viewers also liked (20)

PDF
2015 SW마에스트로 100+ 컨퍼런스_실리콘밸리부터 테헤란로까지
PDF
CoderDojo Romagna
PPTX
Hands on django part 1
PDF
Filling the flask
PDF
Intro python-object-protocol
KEY
Quattro passi tra le nuvole (e non scordate il paracadute)
PDF
Diabetes and Me: My Journey So Far
KEY
Fuga dalla Comfort Zone
PDF
Python Static Analysis Tools
PDF
Online / Offline
PDF
Introduction to SQLAlchemy and Alembic Migrations
PDF
Coderfaire Data Networking for Developers
PPT
Flask - Python microframework
PDF
Introduction to SQLAlchemy ORM
PDF
We Are All Remote Workers
PDF
Impact of Restful Web Architecture on Performance and Scalability
PDF
Web develop in flask
PDF
Selenium testing
PDF
Django channels
PDF
Rest api with Python
2015 SW마에스트로 100+ 컨퍼런스_실리콘밸리부터 테헤란로까지
CoderDojo Romagna
Hands on django part 1
Filling the flask
Intro python-object-protocol
Quattro passi tra le nuvole (e non scordate il paracadute)
Diabetes and Me: My Journey So Far
Fuga dalla Comfort Zone
Python Static Analysis Tools
Online / Offline
Introduction to SQLAlchemy and Alembic Migrations
Coderfaire Data Networking for Developers
Flask - Python microframework
Introduction to SQLAlchemy ORM
We Are All Remote Workers
Impact of Restful Web Architecture on Performance and Scalability
Web develop in flask
Selenium testing
Django channels
Rest api with Python
Ad

Similar to REST Web API with MongoDB (20)

PDF
MongoDB: a gentle, friendly overview
KEY
MongoDB at ZPUGDC
KEY
MongoDB, PHP and the cloud - php cloud summit 2011
PDF
Introduction to MongoDB
PDF
MongoDB at FrozenRails
PDF
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
PPTX
Dev Jumpstart: Build Your First App with MongoDB
PPT
Meetup#1: 10 reasons to fall in love with MongoDB
KEY
Mongodb intro
KEY
MongoDB at RuPy
PPT
9. Document Oriented Databases
PDF
Build your first MongoDB App in Ruby @ StrangeLoop 2013
KEY
PDF
FrozenRails Training
KEY
MongoDB NYC Python
PDF
Mongo learning series
KEY
MongoDB EuroPython 2009
PDF
MongoDB in FS
PPTX
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
PPTX
Marc s01 e02-crud-database
MongoDB: a gentle, friendly overview
MongoDB at ZPUGDC
MongoDB, PHP and the cloud - php cloud summit 2011
Introduction to MongoDB
MongoDB at FrozenRails
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
Dev Jumpstart: Build Your First App with MongoDB
Meetup#1: 10 reasons to fall in love with MongoDB
Mongodb intro
MongoDB at RuPy
9. Document Oriented Databases
Build your first MongoDB App in Ruby @ StrangeLoop 2013
FrozenRails Training
MongoDB NYC Python
Mongo learning series
MongoDB EuroPython 2009
MongoDB in FS
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Marc s01 e02-crud-database

More from MongoDB (20)

PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
PDF
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
PDF
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
PDF
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
PDF
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
PDF
MongoDB SoCal 2020: MongoDB Atlas Jump Start
PDF
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
PDF
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
PDF
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
PDF
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
PDF
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
PDF
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
PDF
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
PDF
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
PDF
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
PDF
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Unlocking AI with Model Context Protocol (MCP)
PPT
Teaching material agriculture food technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Mobile App Security Testing_ A Comprehensive Guide.pdf
Network Security Unit 5.pdf for BCA BBA.
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Unlocking AI with Model Context Protocol (MCP)
Teaching material agriculture food technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Digital-Transformation-Roadmap-for-Companies.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Spectral efficient network and resource selection model in 5G networks
Review of recent advances in non-invasive hemoglobin estimation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
20250228 LYD VKU AI Blended-Learning.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Advanced methodologies resolving dimensionality complications for autism neur...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

REST Web API with MongoDB

  • 1. RESTful Web API with MongoDB Nicola Iarocci
  • 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 Old School 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 & 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”});
  • 14. JSON & RESTful API FILTERING & SORTING ?where={x: 3, y: "foo”} Client API Mongo (very) thin native parsing JSON Mongo & validation (BSON) query syntax layer
  • 15. JSON all along the pipeline mapping to and from the database feels more natural
  • 16. Schema-less dynamic objects allow for a painless evolution of our schema (because yes, a schema exists at any point in time)
  • 17. ORM Where we’re going we 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 entry point + 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
  • 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
  • 32. Thank you. english: @nicolaiarocci italian: nicolaiarocci.com