Slide 1

Slide 1 text

MongoEngine NoORM for NoSQL Serge Matveenko github.com/lig PyCon Russia 2013

Slide 2

Slide 2 text

Quick MongoDB intro NoSQL document-oriented database ● scalable (auto-sharding, replication) ● high-performance (~104-105 queries/sec) ● open source (db: AGPL, drivers: Apache)

Slide 3

Slide 3 text

RDBMS vs MongoDB RDBMS MongoDB data DB + Driver DB + PyMongo aggregation constraints Application validation cascade updates business logic Application

Slide 4

Slide 4 text

RDBMS vs MongoDB RDBMS MongoDB data DB + Driver DB + PyMongo aggregation constraints MongoEngine validation cascade updates business logic Application Application

Slide 5

Slide 5 text

MongoEngine class CharacterName(EmbeddedDocument): first = StringField() last = StringField() class Character(Document): name = EmbeddedDocumentField(CharacterName) rating = IntField() MongoDB { "_id" : ObjectId ("5125e0ee98764119e77d9b1f"), "_types" : ["Character"], "rating" : 42, "name" : {"_types" : ["CharacterName"], "last": "Connor", "_cls": "CharacterName", "first": "Sarah"}, "_cls": "Character" } How it looks

Slide 6

Slide 6 text

MongoEngine class CharacterName(EmbeddedDocument): first = StringField() last = StringField() class Character(Document): name = EmbeddedDocumentField(CharacterName) rating = IntField() MongoDB { "_id" : ObjectId ("5125e0ee98764119e77d9b1f"), "rating" : 42, "name" : {"last": "Connor", "_cls": "CharacterName", "first": "Sarah"}, "_cls": "Character" } How it looks (MongoEngine 0.8)

Slide 7

Slide 7 text

Simple db.character.find({'name.first': 'John'}) Character.objects(name__first='John') db.character.find({rating: {$gte: 42}}).limit(5) Character.objects(rating__gte=42)[:5] db.character.find({rating: {$exists: false}}) Character.objects(rating__exists=False) Querying with MongoEngine

Slide 8

Slide 8 text

Map/Reduce db.character.mapReduce(map_f, reduce_f, {out: 'mr_col'}); db.mr_col.find(); Character.objects.map_reduce(map_f, reduce_f, 'mr_col') Querying with MongoEngine

Slide 9

Slide 9 text

Creating db.character.save( {name: {first: 'John', last: 'Conor'}, rating: 42}) jc = Character( name=CharacterName(first='John', last='Conor'), rating=42) jc.save() Updating with MongoEngine

Slide 10

Slide 10 text

Atomic updates db.character.update( {name: {first: "John", last: "Conor"}}, {$set: {"name.last": "Connor"}}) Character.objects( name=CharacterName(first='John', last='Conor') ).update(set__name__last='Connor') Updating with MongoEngine

Slide 11

Slide 11 text

Under the hood MongoDB "C" BSON Module PyMongo MongoEngine JavaScript Console Binary data Python native types Declarative classes

Slide 12

Slide 12 text

Under the hood MongoDB "C" BSON Module PyMongo MongoEngine JavaScript Console Binary data Python native types Declarative classes

Slide 13

Slide 13 text

Performance

Slide 14

Slide 14 text

Out of the box ● authentication backend ● session engine ● GridFSStorage Third party ● stephrdev/django-mongoforms (Model Forms port) ● wpjunior/django-mongotools (forms, generic views) ● lig/django-registration-me (django-registration port) Django integration

Slide 15

Slide 15 text

Customizing things (added today. sorry:) ● SequenceField(value_decorator=func) ● StringField(regex=r'…') ● URLField(verify_exists=True) ● … ● Inherit from standard Field classes ● Write your own Field classes ● Inherit from BaseDocument class, don't forget metaclass! ● Any metamagic your like…

Slide 16

Slide 16 text

Questions? Serge Matveenko (github.com/lig) mongoengine.org www.mongodb.org Thanks to Ross Lawley (github.com/rozza) Elena Voronina :)