Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MongoEngine - NoORM for NoSQL

MongoEngine - NoORM for NoSQL

The talk about MongoEngine presented on PyCon Russia 2013.

Serge Matveenko

February 24, 2013
Tweet

More Decks by Serge Matveenko

Other Decks in Programming

Transcript

  1. Quick MongoDB intro NoSQL document-oriented database • scalable (auto-sharding, replication)

    • high-performance (~104-105 queries/sec) • open source (db: AGPL, drivers: Apache)
  2. RDBMS vs MongoDB RDBMS MongoDB data DB + Driver DB

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

    + PyMongo aggregation constraints MongoEngine validation cascade updates business logic Application Application
  4. 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
  5. 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)
  6. 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
  7. 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
  8. Under the hood MongoDB "C" BSON Module PyMongo MongoEngine JavaScript

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

    Console Binary data Python native types Declarative classes
  10. 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
  11. 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…