Professional Documents
Culture Documents
Django Mon God B Engine
Django Mon God B Engine
Before starting with mongodb specific topics it’s important to know that we don’t
dislike relational databases, we know they are good for many things but we also know
that web applications success is mainly based on their performance and speed so
that’s what we’re running after and that’s why we’re all here.
Django MongoDB Backend
Existing Technologies
”What matters is who adapts faster to the changing conditions” - Charles Darwin
Joins: The best thing you can do here is forget about JOINS
ForeignKeys: We have DBRef but, do we really need them?
M2M?: What about dicionaries/maps and lists/arrays?
And last but not least, If you really need to do a query that joins 2 collections
based on a field reference that should handle a many to many relation then you
have map/reduce.
Django MongoDB Backend
Moving Forward
SQL to MongoDB Query Translation. . .
”What matters is who adapts faster to the changing conditions” - Charles Darwin
Joins: The best thing you can do here is forget about JOINS
ForeignKeys: We have DBRef but, do we really need them?
M2M?: What about dicionaries/maps and lists/arrays?
And last but not least, If you really need to do a query that joins 2 collections
based on a field reference that should handle a many to many relation then you
have map/reduce.
Django MongoDB Backend
Moving Forward
SQL to MongoDB Query Translation. . .
”What matters is who adapts faster to the changing conditions” - Charles Darwin
Joins: The best thing you can do here is forget about JOINS
ForeignKeys: We have DBRef but, do we really need them?
M2M?: What about dicionaries/maps and lists/arrays?
And last but not least, If you really need to do a query that joins 2 collections
based on a field reference that should handle a many to many relation then you
have map/reduce.
Django MongoDB Backend
Moving Forward
SQL to MongoDB Query Translation. . .
”What matters is who adapts faster to the changing conditions” - Charles Darwin
Joins: The best thing you can do here is forget about JOINS
ForeignKeys: We have DBRef but, do we really need them?
M2M?: What about dicionaries/maps and lists/arrays?
And last but not least, If you really need to do a query that joins 2 collections
based on a field reference that should handle a many to many relation then you
have map/reduce.
Django MongoDB Backend
Moving Forward
Keeping things lazy
It is important when getting orms to work with mongodb that we keep things lazy to
avoid bottle necks in our web applications. Mongodb doesn’t have many to many
relations but it has lists and dictionaries. For example
class User(models.Model):
nickname = models.CharField(max length=255)
full name = models.CharField(max length=255)
friends = ListField()
groups = ListField()
Django MongoDB Backend
Moving Forward
Keeping things lazy
It is important when getting orms to work with mongodb that we keep things lazy to
avoid bottle necks in our web applications. Mongodb doesn’t have many to many
relations but it has lists and dictionaries. For example
class User(models.Model):
nickname = models.CharField(max length=255)
full name = models.CharField(max length=255)
friends = ListField()
groups = ListField()
Django MongoDB Backend
Moving Forward
Keeping Relations or Embedding?
This is a common question when moving from relational databases to non-rel ones.
Should we keep our models related or embed smallest ones into the biggest ones?. The
answer is NO, you shouldn’t keep them related. For Example, A common situation (or
commonly used to show how mongodb works) is a blog engine with posts and
comments. Lets see how we could handle comments (not threaded) in our blog engine:
Django MongoDB Backend
Moving Forward
Keeping Relations or Embedding?
Using References
class Comment(models.Model):
post = models.ForeignKey(Post)
user = models.ForeignKey(User)
text = models.CharField(max_length=255)
my comment = Comment.objects.get_or_create(**kwargs)[0]
Django MongoDB Backend
Moving Forward
Keeping Relations or Embedding?
Without References
class Post(models.Model):
...
comments = ListField()
Without References
class Post(models.Model):
...
comments = ListField()
Without References
class Post(models.Model):
...
comments = ListField()
Without References
class Post(models.Model):
...
comments = ListField()
Let’s improve our Comment model (in case we decided to have some relations).
class Comment(models.Model):
post = models.ForeignKey(Post)
user = GenericField()
text = models.CharField(max_length=255)
my comment = Comment.objects.get_or_create(**kwargs)[0]
Django MongoDB Backend
Moving Forward
Taking Advantage from schema-less
Let’s improve our Comment model (in case we decided to have some relations).
class Comment(models.Model):
post = models.ForeignKey(Post)
user = GenericField()
text = models.CharField(max_length=255)
my comment = Comment.objects.get_or_create(**kwargs)[0]
Django MongoDB Backend
Summary
What we’ve learned. . .
Thanks!
Questions?