REST Web API with MongoDB

32
RESTful Web API with MongoDB Nicola Iarocci

description

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

Transcript of REST Web API with MongoDB

Page 1: REST Web API with MongoDB

RESTful Web APIwith MongoDB

Nicola Iarocci

Page 2: REST Web API with MongoDB

Good Morning.

Page 3: REST Web API with MongoDB

Il Piccolo Libro di MongoDB

by Karl Seguin, traduzione di

Nicola Iaroccihttp://nicolaiarocci.com/il-piccolo-libro-di-mongodb-edizione-italiana/

Italian edition

Page 5: REST Web API with MongoDB

Your Typical Old School Desktop app...

... now going web & mobile

Page 6: REST Web API with MongoDB

MongoDBand REST

or why we picked MongoDB

for our REST API

Page 7: REST Web API with MongoDB

made NoSQL easy to grasp (even for a dumbhead like me)

Similarity with RDBMS

Page 8: REST Web API with MongoDB

Terminology

RDBMS Mongo

Database Database

Table Collection

Rows(s) JSON Document

Index Index

Join Embedding & Linking

Page 9: REST Web API with MongoDB

true selling point for me

JSON-style data store

Page 10: REST Web API with MongoDB

JSON & RESTful API

JSONaccepted media type

Client

JSON(BSON)

Mongo

GET

maybe we can push directly to client?

Page 11: REST Web API with MongoDB

JSON & RESTful API

JSONaccepted media type

Client

JSON(BSON)

Mongo

JSON/dictmaps to python dict

API

GET

almost.

Page 12: REST Web API with MongoDB

JSON & RESTful API

JSONobjects

Client

JSON(BSON)

Mongo

JSON/dictmaps to python dict

(validation layer)

API

POST

also works when posting (adding) items to the database

Page 13: REST Web API with MongoDB

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

Page 14: REST Web API with MongoDB

JSON & RESTful API

nativeMongoquery syntax

Client

JSON(BSON)

Mongo

(very) thin parsing

& validation layer

API

FILTERING & SORTING

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

Page 15: REST Web API with MongoDB

mapping to and from the database feels more natural

JSON all along the pipeline

Page 16: REST Web API with MongoDB

dynamic objects allow for a painless evolution of our schema (because yes, a schema exists at any point in time)

Schema-less

Page 17: REST Web API with MongoDB

Where we’re going we don’t need ORMs.

ORM

Page 18: REST Web API with MongoDB

official Python driver MongoDB drivers are beautiful

PyMongo

Page 19: REST Web API with MongoDB

Also in MongoDB

• setup is a breeze

• lightweight

• fast inserts, updates and queries

• excellent documentation

• great support by 10gen

• great community

Page 20: REST Web API with MongoDB

CollectionsAPI’s entry point + plural nounshttp://api.example.com/v1/contacts

Page 21: REST Web API with MongoDB

And eventually by an alternative lookup value

Document

http://api.example.com/v1/contacts/CUST12345

Documents are identified by ObjectIDhttp://api.example.com/v1/contacts/4f46445fc88e201858000000

Page 22: REST Web API with MongoDB

def get_collection(collection): documents = [] cursor = db(collection).find(**args) for document in cursor: documents.append(document) return documents

Collection GET

find() accepts a python dict as query expression, and returns a cursor we can

iterate

http://api.example.com/v1/contacts?where={“age”: {“$gt”: 20}}

Page 23: REST Web API with MongoDB

Editing a Resource

PATCH

Page 24: REST Web API with MongoDB

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})

Page 25: REST Web API with MongoDB

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})

Page 26: REST Web API with MongoDB

PATCHingdef patch_document(collection, original): (...) # Perform the update db(collection).update({"_Id": ObjectId(object_id)},

{"$set": updates})

$set accepts a dictwith the updates for the db

eg: {“active”: False}.

updates are atomic

Page 27: REST Web API with MongoDB

Creating Resources

POST

Page 28: REST Web API with MongoDB

def post(collection): (...) for key, item in docs.items(): response[ID_FIELD] = db(collection).insert(item)

POSTing

push document and get its ObjectId back from Mongo.

like other CRUD operations, inserting is trivial in

mongo.

Page 29: REST Web API with MongoDB

IntroducingEve

Open Source RESTful Web APIpowered by MongoDB

Page 30: REST Web API with MongoDB

... stay tuned!

work in

progress!

get the

full s

tory

here!

Page 31: REST Web API with MongoDB

Wanna see it running?Hopefully it won’t explode right into my face

Page 32: REST Web API with MongoDB

Thank you.

english: @nicolaiarocciitalian: nicolaiarocci.com