Post on 10-Apr-2018
8/8/2019 Django Mon God b Engine
1/31
Django MongoDB BackendUsing Django and Non-relational for Web Development
Flavio Percoco Premoliflaper87@flaper87.org
Research & Development, The Net Planet Europe
October 04th, 2010
mailto:flaper87@flaper87.orgmailto:flaper87@flaper87.org8/8/2019 Django Mon God b Engine
2/31
Django MongoDB Backend
Introduction
Disclaimer
Before starting with mongodb specific topics its important to know that we dontdislike relational databases, we know they are good for many things but we also knowthat web applications success is mainly based on their performance and speed sothats what were running after and thats why were all here.
8/8/2019 Django Mon God b Engine
3/31
Django MongoDB Backend
Existing Technologies
MongoKit (Nicolas Clairon)
mongoengine (Harry Marr)django-mongodb-engine (Flavio Percoco Premoli and Alberto Paro)
Dj M DB B k d
8/8/2019 Django Mon God b Engine
4/31
Django MongoDB Backend
Existing Technologies
MongoKit (Nicolas Clairon)
mongoengine (Harry Marr)django-mongodb-engine (Flavio Percoco Premoli and Alberto Paro)
Dj M DB B k d
8/8/2019 Django Mon God b Engine
5/31
Django MongoDB Backend
Existing Technologies
MongoKit (Nicolas Clairon)
mongoengine (Harry Marr)django-mongodb-engine (Flavio Percoco Premoli and Alberto Paro)
Django MongoDB Backend
8/8/2019 Django Mon God b Engine
6/31
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 collectionsbased on a field reference that should handle a many to many relation then youhave map/reduce.
Django MongoDB Backend
8/8/2019 Django Mon God b Engine
7/31
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 collectionsbased on a field reference that should handle a many to many relation then youhave map/reduce.
Django MongoDB Backend
8/8/2019 Django Mon God b Engine
8/31
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 collectionsbased on a field reference that should handle a many to many relation then youhave map/reduce.
Django MongoDB Backend
8/8/2019 Django Mon God b Engine
9/31
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 collectionsbased on a field reference that should handle a many to many relation then youhave map/reduce.
Django MongoDB Backend
8/8/2019 Django Mon God b Engine
10/31
j g g
Moving Forward
Keeping things lazy
Yes, because were lazy people so we do lazy things. . .
It is important when getting orms to work with mongodb that we keep things lazy toavoid bottle necks in our web applications. Mongodb doesnt have many to manyrelations 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
8/8/2019 Django Mon God b Engine
11/31
j g g
Moving Forward
Keeping things lazy
Yes, because were lazy people so we do lazy things. . .
It is important when getting orms to work with mongodb that we keep things lazy toavoid bottle necks in our web applications. Mongodb doesnt have many to manyrelations 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
8/8/2019 Django Mon God b Engine
12/31
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 shouldnt keep them related. For Example, A common situation (orcommonly used to show how mongodb works) is a blog engine with posts andcomments. Lets see how we could handle comments (not threaded) in our blog engine:
Django MongoDB Backend
8/8/2019 Django Mon God b Engine
13/31
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)
kwargs = { post : my post,
user : my user,
text : my text,
defaults : {}}
my comment = Comment.objects.get_or_create(**kwargs)[0]
Django MongoDB Backend
8/8/2019 Django Mon God b Engine
14/31
Moving Forward
Keeping Relations or Embedding?
Without References
class Post(models.Model):
...
comments = ListField()
post.comments.append({ user : user,text : text }
post.save()
Embedded? Not a good idea.
ForeignKey? If were using RDBMs.Dictionary? Good idea but slow.
Django MongoDB BackendM i F d
8/8/2019 Django Mon God b Engine
15/31
Moving Forward
Keeping Relations or Embedding?
Without References
class Post(models.Model):
...
comments = ListField()
post.comments.append({ user : user,text : text }
post.save()
Embedded? Not a good idea.
ForeignKey? If were using RDBMs.Dictionary? Good idea but slow.
Django MongoDB BackendMo i g Fo a d
8/8/2019 Django Mon God b Engine
16/31
Moving Forward
Keeping Relations or Embedding?
Without References
class Post(models.Model):
...
comments = ListField()
post.comments.append({ user : user,text : text }
post.save()
Embedded? Not a good idea.
ForeignKey? If were using RDBMs.Dictionary? Good idea but slow.
Django MongoDB BackendMoving Forward
8/8/2019 Django Mon God b Engine
17/31
Moving Forward
Keeping Relations or Embedding?
Without References
class Post(models.Model):
...
comments = ListField()
post.comments.append({ user : user,text : text }
post.save()
Embedded? Not a good idea.
ForeignKey? If were using RDBMs.Dictionary? Good idea but slow.
Django MongoDB BackendMoving Forward
8/8/2019 Django Mon God b Engine
18/31
Moving Forward
Taking Advantage from schema-less
Schema-less databases do have a structureDynamicNot typed
Most of then are json-like maps.Great for web development
No Migrations needed.No IntegrityErrors.
Django MongoDB BackendMoving Forward
8/8/2019 Django Mon God b Engine
19/31
Moving Forward
Taking Advantage from schema-less
Schema-less databases do have a structureDynamicNot typed
Most of then are json-like maps.Great for web development
No Migrations needed.No IntegrityErrors.
Django MongoDB BackendMoving Forward
8/8/2019 Django Mon God b Engine
20/31
g
Taking Advantage from schema-less
Schema-less databases do have a structureDynamicNot typed
Most of then are json-like maps.Great for web development
No Migrations needed.No IntegrityErrors.
Django MongoDB BackendMoving Forward
8/8/2019 Django Mon God b Engine
21/31
Taking Advantage from schema-less
Schema-less databases do have a structureDynamicNot typed
Most of then are json-like maps.Great for web development
No Migrations needed.No IntegrityErrors.
Django MongoDB BackendMoving Forward
8/8/2019 Django Mon God b Engine
22/31
Taking Advantage from schema-less
Schema-less databases do have a structureDynamicNot typed
Most of then are json-like maps.Great for web development
No Migrations needed.No IntegrityErrors.
Django MongoDB BackendMoving Forward
8/8/2019 Django Mon God b Engine
23/31
Taking Advantage from schema-less
Schema-less databases do have a structureDynamicNot typed
Most of then are json-like maps.Great for web development
No Migrations needed.No IntegrityErrors.
Django MongoDB BackendMoving Forward
8/8/2019 Django Mon God b Engine
24/31
Taking Advantage from schema-less
Lets 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 user = "FlaPer87" #Known User
my user = { username : FlaPer87,
email : flaper87@flaper87.org,url :http://blog.flaper87.org/}#Anonymous
kwargs = { post : my post,
user : my user,
text : my text,
defaults : {}}
my comment = Comment.objects.get_or_create(**kwargs)[0]
Django MongoDB BackendMoving Forward
T ki Ad f h l
8/8/2019 Django Mon God b Engine
25/31
Taking Advantage from schema-less
Lets 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 user = "FlaPer87" #Known User
my user = { username : FlaPer87,
email : flaper87@flaper87.org,url :http://blog.flaper87.org/}#Anonymous
kwargs = { post : my post,
user : my user,
text : my text,
defaults : {}}
my comment = Comment.objects.get_or_create(**kwargs)[0]
Django MongoDB BackendSummary
Wh t l d
8/8/2019 Django Mon God b Engine
26/31
What weve learned.. .
Re-model your models
Be Lazy to be faster
Forget about relations, they will slow you down
Remember that dynamism is better than restrictions
Django MongoDB BackendSummary
What weve learned
8/8/2019 Django Mon God b Engine
27/31
What we ve learned.. .
Re-model your models
Be Lazy to be faster
Forget about relations, they will slow you down
Remember that dynamism is better than restrictions
Django MongoDB BackendSummary
What weve learned
8/8/2019 Django Mon God b Engine
28/31
What we ve learned.. .
Re-model your models
Be Lazy to be faster
Forget about relations, they will slow you down
Remember that dynamism is better than restrictions
Django MongoDB BackendSummary
What weve learned
8/8/2019 Django Mon God b Engine
29/31
What we ve learned.. .
Re-model your models
Be Lazy to be faster
Forget about relations, they will slow you down
Remember that dynamism is better than restrictions
Django MongoDB BackendThe End
A little bit about me
8/8/2019 Django Mon God b Engine
30/31
Full Name: Flavio Percoco Premoli
IRC: FlaPer87 at irc.freenode.net
Twitter: FlaPer87
blog: http://www.flaper87.org
e-mail: flaper87@flaper87.org
Anywhere else: FlaPer87Short Background:
GNOME a11y and Open a11y Contributor (MouseTrap[http://live.gnome.org/MouseTrap]).
Open Source Developer/Contributor (Web and Desktop).
MongoDB Community Member and Contributor.
R&D Developer at The Net Planet Europe.
Knowledge Management SystemsSearch EnginesIndexing EnginesCloud ComputingNoSQL Technologies
Linux Lover/User and Mac user too
Django MongoDB BackendThe End
Interaction
http://www.flaper87.org/mailto:flaper87@flaper87.orgmailto:flaper87@flaper87.orghttp://www.flaper87.org/8/8/2019 Django Mon God b Engine
31/31
Thanks!
Questions?