By Andre Medeiros at DjangoCon Finland 2013
Andre Medeiros
Founder at Iroquote.com
Next generation discussion forums
Brazilian that doesn't play soccer
M.Sc. from Aalto University
fooman = {"name" : "Foo", "age" : 25};
db.users.save(fooman);
# Python
person = {
'name': "John Doe",
'skills': ["Javascript", "CSS", "HTML"],
'work': None
}
// Javascript
person = {
"name" : "John Doe",
"skills" : ["Javascript", "CSS", "HTML"],
"work" : null
};
Writing Python is fun, and keeping everything in Python limits the number of times your brain has to do a "context switch." It helps productivity if you keep yourself in a single programming environment/mentality for as long as possible.—Djangobook.com
If MongoDB had existed 10 years ago the vast majority of data used to drive modern web applications would be stored in MongoDB and not a relational database.—Will Shulman, CEO & Co-founder of MongoLab
{
"_id" : ObjectId("5500165de50c050005e4b29f"),
"title" : "Using MongoDB as your primary Django database",
"message" : "This presentation introduces MongoDB to Django de...",
"author" : ObjectId("5182953ba50c051575e4b29c"),
"creation_date" : ISODate("2013-05-21T12:44:31.408Z"),
"tags" : ["django","python","mongodb","mongoengine","nosql"],
"comments" : [
{
"author" : ObjectId("50c90b3128650a004200961a"),
"message" : "Nice, I didn't know this was possible."
}
],
}
At the core of Django is its ORM
MongoEngine
is an Object-Document Mapper made for MongoDB, following Django's ORM style.
pip install mongoengine==0.8.0
from mongoengine import *
class Choice(EmbeddedDocument):
choice_text = StringField(max_length=200)
votes = IntField(default=0)
class Poll(Document):
question = StringField(max_length=200)
pub_date = DateTimeField(help_text='date published')
choices = ListField(EmbeddedDocumentField(Choice))
{
"_id" : ObjectId("5483165de50c050005e4b29f"),
"question" : "What's up?",
"pub_date" : ISODate("2013-04-14T11:06:21.922Z"),
"choices" : [
{
"choice_text" : "Not much",
"votes" : 0
},
{
"choice_text" : "Just hacking again",
"votes" : 1
}
],
}
from yourproject.models import Poll, Choice
poll = Poll.objects(question__contains="What").first()
choice = Choice(choice_text="I'm at DjangoCon.fi", votes=23)
poll.choices.append(choice)
poll.save()
print poll.question
class Poll(Document):
question = StringField(max_length=200)
pub_date = DateTimeField(help_text='date published')
choices = ListField(EmbeddedDocumentField(Choice))
meta = {
'indexes': [
'question',
('pub_date', '+question')
]
}
python manage.py syncdb
import mongoengine
DATABASES = {
'default': {
'ENGINE': '',
},
}
SESSION_ENGINE = 'mongoengine.django.sessions' # optional
_MONGODB_USER = 'mongouser'
_MONGODB_PASSWD = 'password'
_MONGODB_HOST = 'thehost'
_MONGODB_NAME = 'thedb'
_MONGODB_DATABASE_HOST = \
'mongodb://%s:%s@%s/%s' \
% (_MONGODB_USER, _MONGODB_PASSWD, _MONGODB_HOST, _MONGODB_NAME)
mongoengine.connect(_MONGODB_NAME, host=_MONGODB_DATABASE_HOST)
AUTHENTICATION_BACKENDS = (
'mongoengine.django.auth.MongoEngineBackend',
)
import mongoengine
# ...
user = authenticate(username=username, password=password)
assert isinstance(user, mongoengine.django.auth.User)
from django.test.simple import DjangoTestSuiteRunner
from django.test import TestCase
class NoSQLTestRunner(DjangoTestSuiteRunner):
def setup_databases(self):
pass
def teardown_databases(self, *args):
pass
class NoSQLTestCase(TestCase):
def _fixture_setup(self):
pass
def _fixture_teardown(self):
pass
TEST_RUNNER = 'yourproject.tests.NoSQLTestRunner'