Django Mongodb Engine Readthedocs Io en Latest
Django Mongodb Engine Readthedocs Io en Latest
Release
1 Contents 3
1.1 Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.3 Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.4 Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.5 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.6 Meta . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
i
ii
Django MongoDB Engine, Release
Django MongoDB Engine is a MongoDB backend for Django, the Python Web framework for perfectionists with
deadlines.
This documentation is split into several sections:
• The tutorial introduces you to non-relational schema design and tools provided by Django MongoDB Engine to
put that design into action.
• Our topical guides explain every feature of Django MongoDB Engine in great detail – they’ll provide you with
usage guides, API overviews, code samples etc.
• The API reference has all the lower-level details on functions, methods, classes, settings, modules and whatnot.
• In the Meta section, we collect information about the project itself: What has changed between versions, how
development happens and how you can get involved.
Contents 1
Django MongoDB Engine, Release
2 Contents
CHAPTER 1
Contents
Tutorial
The goal of this step-by-step tutorial is to introduce you to non-relational schema design and the tools Django Mon-
goDB Engine provides to put that design into action.
This tutorial assumes that you are already familiar with Django and have a basic idea of MongoDB operation and a
configured MongoDB installation.
Our example project covers the development of a simple single-user blog application with tag and comment capabili-
ties.
Note: When you try out the shell examples given in this tutorial (which you should!) it is important to remember that
model changes will have no effect until you restart the shell.
If you come from a relational background a non-relational database may seem counter-intuitive or completely odd
since most non-relational databases are document and multi-key oriented and provide a different method of querying
and excluding data.
Perhaps a good way to get used to non-relational style data modeling is to ask yourself “What would I never do in
SQL”.
Because most relational databases lack proper list data structures you may typically model the Posts Tags Comments
relationship using three models/tables, one table per entity type.
Organizing your data using multiple relationships is the exact opposite of what we will do for our non-relational data
model: Have one single collection (table) for everything and store tags and comments in simple lists.
Here is a simple model for blog posts, designed for non-relational databases:
3
Django MongoDB Engine, Release
class Post([Link]):
title = [Link]()
text = [Link]()
tags = ListField()
comments = ListField()
Let’s try this out. Fire up a Django shell and add a post:
>>> from [Link] import Post
>>> post = [Link](
... title='Hello MongoDB!',
... text='Just wanted to drop a note from Django. Cya!',
... tags=['mongodb', 'django']
... )
>>> [Link]
[]
>>> [Link](['Great post!', 'Please, do more of these!'])
>>> [Link]()
>>> [Link]().comments
[u'Great post!', u'Please, do more of these!']
In the MongoDB shell, you can see how the resulting data record looks like:
{
"_id" : ObjectId("..."),
"tags" : ["mongodb", "django"],
"text" : "Just wanted to drop a note from Django. Cya!",
"title" : "Hello MongoDB!",
"comments" : [
"Great post!",
"Please, do more of these!"
]
}
You may have noticed something’s missing from the Post class: We have no information about the date and time our
posts are created! Fixed easily.
Happily, because MongoDB is schema-less, we can add new fields to our model without corrupting existing data
records (“documents”). Forget about migrations!
So, adding a new field boils down to... adding a new field.
class Post([Link]):
created_on = [Link](auto_now_add=True, null=True) # <---
4 Chapter 1. Contents
Django MongoDB Engine, Release
title = [Link](max_length=255)
text = [Link]()
tags = ListField()
comments = ListField()
One thing to keep in mind is what happens to our old posts: Because they miss a created_on value, when fetching
them in Django, the created_on attribute will be set to the DateTimeField default value, None. To allow None
as value, we have to pass null=True.
There’s another flaw in our design: We can’t store any comment meta information like author name/email and creation
time. We’ll tackle that in the next section.
Embedded Models
So far, we used to store comments as a list of strings. We’ll have to rework that design in order to store additional
information for each comment.
Let’s first design our model for comments.
class Comment([Link]):
created_on = [Link](auto_now_add=True)
author_name = [Link](max_length=255)
author_email = [Link]()
text = [Link]()
{
'created_on': ISODate('...'),
'author_name': 'Bob',
'author_email': 'bob@[Link]',
'text': 'The cake is a lie'
}
MongoDB allows to have objects within objects – called “subobjects” or “embedded objects” – so we could also
represent this as follows:
{
'created_on': ISODate('...'),
'author' : {
'name': 'Bob',
'email': 'bob@[Link]'
},
'text' : 'The cake is a lie'
}
1.1. Tutorial 5
Django MongoDB Engine, Release
Django itself does not allow such nesting – because there’s no such thing in SQL – but Django MongoDB Engine
provides the tools to do anyway.
To embed instances of models into other models, we can use EmbeddedModelField:
from [Link] import EmbeddedModelField
tags = ListField()
comments = ListField(EmbeddedModelField('Comment')) # <---
class Comment([Link]):
created_on = [Link](auto_now_add=True)
author = EmbeddedModelField('Author')
text = [Link]()
class Author([Link]):
name = [Link]()
In the same way, we can embed Comment objects into the comments list of a blog post, by combining ListField and
EmbeddedModelField:
class Post([Link]):
created_on = [Link](auto_now_add=True, null=True)
title = [Link]()
text = [Link]()
tags = ListField()
comments = ListField(EmbeddedModelField('Comment')) # <---
We should mess around with our new Post model at this point.
>>> Post(
... title='I like cake',
... comments=[comment]
... ).save()
>>> post = [Link](title='I like cake')
>>> [Link]
[<Comment: Comment object>]
>>> [Link][0].[Link]
u'bob@[Link]'
6 Chapter 1. Contents
Django MongoDB Engine, Release
"comments" : [
{
"text" : "The cake is a lie",
"created_on" : ISODate("..."),
"id" : null,
"author" : {
"email" : "bob@[Link]",
"name" : "Bob",
"id" : null
}
}
],
"created_on" : ISODate("...")
}
To make our app actually useful, it’s time to add some views. Here’s how your post overview page could look like:
<h1>Post Overview</h1>
{% for post in post_list %}
<h2><a href="{% url post_detail [Link] %}">{{ [Link] }}</a></h2>
<p>
{{ post.created_on }} |
{{ [Link]|length }} comments |
tagged {{ [Link]|join:', ' }}
</p>
{% endfor %}
By using Django’s Generic Views, we even don’t have to write any views, so all that’s left is mapping URLs to those
templates:
from [Link] import patterns, url
from [Link] import ListView, DetailView
post_detail = DetailView.as_view(model=Post)
post_list = ListView.as_view(model=Post)
1.1. Tutorial 7
Django MongoDB Engine, Release
urlpatterns = patterns('',
url(r'^post/(?P<pk>[a-z\d]+)/$', post_detail, name='post_detail'),
url(r'^$', post_list, name='post_list'),
)
To make our blog less boring, we should add some nice pictures.
As MongoDB disciples, what comes to mind when thinking about storing files? Of course! GridFS!
Django MongoDB Engine provides a Django storage backend for GridFS that allows you to use GridFS like any other
file storage:
gridfs_storage = GridFSStorage()
class FileUpload([Link]):
created_on = [Link](auto_now_add=True)
file = [Link](storage=gridfs_storage, upload_to='/')
if [Link]:
8 Chapter 1. Contents
Django MongoDB Engine, Release
Warning: Serving files through such a view is inefficient and insecure. Never use this in production! There are
much superior tools for serving files out of GridFS, e.g. nginx-gridfs.
1.1. Tutorial 9
Django MongoDB Engine, Release
Using Map/Reduce
Our last quest is to count the number of comments each author has made.
This could be done in plain Django but would be very inefficient because we would have to literally fetch all posts
(with all comments) from the database.
Instead, we’re going to use Map/Reduce to accomplish the task.
Programmer’s introduction to Map/Reduce: The map function gets called for each document and emits one or more
key-value pairs. The reduce function is passed a key and a list of values and reduces them to a single resulting value.
The result of such a Map/Reduce operation is a list of key-value pairs, the keys being those emitted by the map function
and the values those resulting from the reduce function.
Our map function emits a (author, 1) pair for each comment.
10 Chapter 1. Contents
Django MongoDB Engine, Release
function map() {
/* `this` refers to the current document */
[Link](function(comment) {
emit([Link], 1);
});
}
The reduce function sums up all the ones emitted by the map function.
Map/Reduce support is added to Django’s ORM using a custom Manager which is installed to the Post model as
follows:
Ready to Map/Reduce?
------------------------
Kick off the Map/Reduce:
------------------------
>>> pairs = [Link].map_reduce(mapfunc, reducefunc, out='temp',
... delete_collection=True)
>>> for pair in pairs:
... print [Link], [Link]
Alice 9.0
Ann 6.0
Bob 3.0
1.1. Tutorial 11
Django MongoDB Engine, Release
• The fourth argument, delete_collection, tells Django MongoDB Engine to delete the temporary collection passed
as third argument after the Map/Reduce result iterator is exhausted.
• The resulting counts are floats because Javascript does not distinguish between integers and floating point num-
bers.
Lastly, a quick word of warning. Map/Reduce is designed to be used for one-time operations – although it performs
very well, it’s definitely not something you would want to execute on a per-request basis. Don’t use Map/Reduce in
“hot” parts of your code.
This tutorial should’ve given you an idea about how easy it is to combine Django and MongoDB using Django Mon-
goDB Engine to produce simple, scalable applications.
Hopefully you’ve learned something useful for your next Django project that you should begin hacking on now. Go
build something cool, and let us know about it!
You can always come back to this documentation as you need to learn new tricks:
• Our topical guides explain every feature of Django MongoDB Engine in great detail – they’ll provide you with
usage guides, API overviews, code samples etc.
• The API reference has all the lower-level details on functions, methods, classes, settings, modules and whatnot.
• In the Meta section, we collect information about the project itself: What has changed between versions, how
development happens and how you can get involved.
If you need support, don’t hesitate to write to our mailing list.
Also, we’d love to see you getting involved in Django MongoDB Engine’s development!
• Fix the documentation. None of the Django MongoDB Engine developers are native English speakers, so this
docs are probably full of typos and weird, ungrammatical or incomprehensible phrasings. Every typo is worth
reporting!
• Extend and improve the documentation. We appreciate any contribution!
• Blog/write about Django MongoDB Engine, and send us a link to your work.
• Report bugs and feature requests.
• Finally, send pull requests or patches containing bug fixes, new features and code improvements.
Setup
Installation
12 Chapter 1. Contents
Django MongoDB Engine, Release
virtualenv
virtualenv myproject
source myproject/bin/activate
Django-nonrel
djangotoolbox
Configuration
Database setup is easy (see also the Django database setup docs):
DATABASES = {
'default' : {
'ENGINE' : 'django_mongodb_engine',
'NAME' : 'my_database'
}
}
Django MongoDB Engine also takes into account the HOST, PORT, USER, PASSWORD and OPTIONS settings.
Possible values of OPTIONS are described in the settings reference.
Done!
That’s it! You can now go straight ahead developing your Django application as you would do with any other database.
1.2. Setup 13
Django MongoDB Engine, Release
Topics
Django MongoDB Engine provides two fields for storing arbitrary (BSON-compatible) Python list and dict ob-
jects in Django model objects, ListField and DictField, which can be used to store information that is not worth a
separate model or that should be queryable in efficient manner (using an index).
Both fields may optionally be provided with type information. That restricts their usage to one single type but has the
advantage of automatic type checks and conversions.
ListField
Stores Python lists (or any other iterable), represented in BSON as arrays.
from [Link] import ListField
class Post([Link]):
...
tags = ListField()
The typed variant automatically does type conversions according to the given type:
class Post([Link]):
...
edited_on = ListField([Link]())
As described in the tutorial, ListFields are very useful when used together with Embedded Models to store lists of
sub-entities to model 1-to-n relationships:
from [Link] import EmbeddedModelField, ListField
class Post([Link]):
...
comments = ListField(EmbeddedModelField('Comment'))
class Comment([Link]):
...
text = [Link]()
Please head over to the Embedded Models topic for more about embedded models.
SetField
Much like a ListField except that it’s represented as a set on Python side (but stored as a list on MongoDB due to the
lack of a separate set type in BSON).
14 Chapter 1. Contents
Django MongoDB Engine, Release
DictField
Stores Python dicts (or any dict-like iterable), represented in BSON as subobjects.
class Image([Link]):
...
exif = DictField()
The typed variant automatically does type conversion on values. (Not on keys as the are required to be strings on
MongoDB.)
class Poll([Link]):
...
votes = DictField([Link]())
DictFields are useful mainly for storing objects of varying shape, i.e. objects whose structure is unknow at coding
time. If all your objects have the same structure, you should consider using Embedded Models.
Embedded Models
Django MongoDB Engine supports MongoDB’s subobjects which can be used to embed an object into another.
Using ListField and DictField it’s already possible to embed objects (dicts) of arbitrary shape.
However, EmbeddedModelField (described beneath) is a much more comfortable tool for many use cases, ensuring
the data you store actually matches the structure and types you want it to be in.
The Basics
class Customer([Link]):
name = [Link](...)
address = EmbeddedModelField('Address')
...
class Address([Link]):
...
city = [Link](...)
The API feels very natural and is similar to that of Django’s relation fields.
1.3. Topics 15
Django MongoDB Engine, Release
{
"_id": ObjectId(...),
"name": "Bob",
"address": {
...
"city": "New York"
},
...
}
While such “flat” embedding is useful if you want to bundle multiple related fields into one common namespace – for
instance, in the example above we bundled all information about a customers’ address into the address namespace –
there’s a much more common usecase for embedded objects: one-to-many relations.
Often, lists of subobjects are superior to relations (in terms of simplicity and performance) for modeling one-to-many
relationships between models.
Consider this elegant way to implement the Post Comments relationship:
class Post([Link]):
...
comments = ListField(EmbeddedModelField('Comment'))
class Comment([Link]):
text = [Link]()
{
"_id": ObjectId(...),
...
"comments" : [
{"text": "foo", },
{"text": "bar"}
]
}
16 Chapter 1. Contents
Django MongoDB Engine, Release
Generic Embedding
Similar to Django’s generic relations, it’s possible to embed objects of any type (sometimes referred to as “polymor-
phic” relationships). This works by adding the model’s name and module to each subobject, accompanying the actual
data with type information:
{
"_id" : ObjectId(...),
"stuff" : [
{"foo" : 42, "_module" : "[Link]", "_model" : "FooModel"},
{"bar" : "spam", "_module" : "[Link]", "_model" : "FooModel"}
]
}
As you can see, generic embedded models add a lot of overhead that bloats up your data records. If you want to use
them anyway, here’s how you’d do it:
class Container([Link]):
stuff = ListField(EmbeddedModelField())
class FooModel([Link]):
foo = [Link]()
class BarModel([Link]):
bar = [Link](max_length=255)
[Link](
stuff=[FooModel(foo=42), BarModel(bar='spam')]
)
Atomic Updates
Django’s support for updates (using the update() method) can be used to run atomic updates against a single or
multiple documents:
results in a update() query that uses the atomic $set operator to update the title field:
It’s also possible to use F() objects which are translated into $inc operations. For example,
[Link](...).update(visits=F('visits')+1)
is translated to:
GridFS
MongoDB’s built-in distributed file system, GridFS, can be used in Django applications in two different ways.
In most cases, you should use the GridFS storage backend provided by Django MongoDB Engine.
1.3. Topics 17
Django MongoDB Engine, Release
Storage
GridFSStorage is a Django storage that stores files in GridFS. That means it can be used with whatever component
makes use of storages – most importantly, FileField.
It uses a special collection for storing files, by default named “storage”.
gridfs = GridFSStorage()
uploads = GridFSStorage(location='/uploads')
Warning: To serve files out of GridFS, use tools like nginx-gridfs. Never serve files through Django in produc-
tion!
Model Field
# DON'T DO THIS
class Bad([Link]):
blob = [Link]()
# NEITHER THIS
class EventWorse([Link]):
blob = [Link](max_length=10*1024*1024)
class Better([Link]):
blob = GridFSField()
A GridFSField may be fed with anything that PyMongo can handle, that is, (preferably) file-like objects and strings.
You’ll always get a GridOut for documents from the database.
18 Chapter 1. Contents
Django MongoDB Engine, Release
Map/Reduce
Map/Reduce, originally invented at Google, is a simple but powerful technology to efficiently process big amounts of
data in parallel.
For this, your processing logic must be split into two phases, the map and the reduce phase.
The map phase takes all the input you’d like to process (in terms of MongoDB, this input are your documents) and
emits one or more key-value pairs for each data record (it “maps” records to key-value pairs).
The reduce phase “reduces” that set of key-value pairs into a single value.
This document explains how to use MongoDB’s Map/Reduce functionality with Django models.
Warning: MongoDB’s Map/Reduce is designed for one-time operations, i.e. it’s not intended to be used in code
that is executed on a regular basis (views, business logic, ...).
How to Use It
Map/Reduce support for Django models is provided through Django MongoDB Engine’s custom Manager (What
is a manager?).
class MapReduceableModel([Link]):
...
objects = MongoDBManager()
The MongoDBManager provides a map_reduce() method that has the same API as PyMongo’s map_reduce()
method (with the one exception that it adds a drop_collection option).
For very small result sets, you can also use in-memory Map/Reduce:
It’s also possible to run Map/Reduce against a subset of documents in the database:
>>> [Link](...).map_reduce(...)
Both the map and the reduce function are written in Javascript.
map_reduce() returns an iterator yielding MapReduceResult objects.
A sane reduce function must be both associative and commutative – that is, in terms of MongoDB, the following
conditions must hold true:
1.3. Topics 19
Django MongoDB Engine, Release
This is because in order to be able to process in parallel, the reduce phase is split into several sub-phases, reducing
parts of the map output and eventually merging them together into one grand total.
Example
(See also the example in the tutorial and Wikipedia, from which I stole the idea for the example beneath.)
As an example, we’ll count the number of occurrences of each word in a bunch of articles. Our models could look
somewhat like this:
class Article([Link]):
author = [Link]('Author')
text = [Link]()
objects = MongoDBManager()
Our map function emits a (word, 1) pair for each word in an article’s text (In the map function, this always refers
to the current document).
function() {
[Link](' ').forEach(
function(word) { emit(word, 1) }
)
}
For an input text of “Django is named after Django Reinhardt”, this would emit the following key-value pairs:
Django : 1
is : 1
named : 1
after : 1
Django : 1
Reinhardt : 1
This pairs are now combined in such way that no key duplicates are left.
is : [1]
named : [1]
after : [1]
Django : [1, 1]
Reinhardt : [1]
To further process these pairs, we let our reduce function sum up all occurrences of each word
20 Chapter 1. Contents
Django MongoDB Engine, Release
is : 1
named : 1
after : 1
Django : 2
Reinhardt : 1
Here’s a full example, using the models and functions described above, on how to use Django MongoDB Engine’s
Map/Reduce API.
from [Link] import models
class Article([Link]):
author = [Link]('Author')
text = [Link]()
objects = MongoDBManager()
class Author([Link]):
pass
mapfunc = """
function() {
[Link](' ').forEach(
function(word) { emit(word, 1) }
)
}
"""
reducefunc = """
function reduce(key, values) {
return [Link]; /* == sum(values) */
}
"""
1.3. Topics 21
Django MongoDB Engine, Release
E 1.0
Caching
Note: This document assumes that you’re already familiar with Django’s caching framework (database caching in
particular).
Django MongoDB Cache is a Django database cache backend similar to the one built into Django (which only works
with SQL databases).
Cache entries are structured like this:
{
"_id" : <your key>,
"v" : <your value>,
"e" : <expiration timestamp>
}
Thanks to MongoDB’s _id lookups being very fast, MongoDB caching may be used as a drop-in replacement for
“real” cache systems such as Memcached in many cases. (Memcached is still way faster and does a better caching job
in general, but the performance you get out of MongoDB should be enough for most mid-sized Web sites.)
Installation
Setup
Please follow the instructions in the Django db cache setup docs for details on how to configure a database cache. Skip
the createcachetable step since there’s no need to create databases in MongoDB. Also, instead of the default
db cache backend name, use "django_mongodb_cache.MongoDBCache" as BACKEND:
CACHES = {
'default' : {
'BACKEND' : 'django_mongodb_cache.MongoDBCache',
'LOCATION' : 'my_cache_collection'
}
}
Django MongoDB Cache will also honor all optional settings the default database cache backend takes care of
(TIMEOUT, OPTIONS, etc).
22 Chapter 1. Contents
Django MongoDB Engine, Release
Aggregations
Django has out-of-the-box support for aggregation. The following aggregations are currently supported by Django
MongoDB Engine:
• Count
• Avg
• Min
• Max
• Sum
MongoDB’s group command is used to perform aggregations using generated Javascript code that implements the
aggregation functions.
While being more flexible than Map/Reduce, a group command can not be processed in parallel, for which reason
you should prefer Map/Reduce to process big data sets.
Warning: Needless to say, you shouldn’t use these aggregations on a regular basis (i.e. in your views or business
logic) but regard them as a powerful tool for one-time operations.
Lower-Level Operations
When you hit the limit of what’s possible with Django’s ORM, you can always go down one abstraction layer to
PyMongo.
You can use raw queries and updates to update or query for model instances using raw Mongo queries, bypassing
Django’s model query APIs.
If that isn’t enough, you can skip the model layer entirely and operate on PyMongo-level objects.
Warning: These APIs are available for MongoDB only, so using any of these features breaks portability to other
non-relational databases (Google App Engine, Cassandra, Redis, ...). For the sake of portability you should try to
avoid database-specific features whenever possible.
MongoDBManager provides two methods, raw_query() and raw_update(), that let you perform raw Mongo
queries.
Note: When writing raw queries, please keep in mind that no field name substitution will be done, meaning that
you’ll always have to use database-level names – e.g. _id instead of id or foo_id instead of foo for foreignkeys.
Raw Queries
raw_query() takes one argument, the Mongo query to execute, and returns a standard Django queryset – which
means that it also supports indexing and further manipulation.
As an example, let’s do some Geo querying.
1.3. Topics 23
Django MongoDB Engine, Release
class Point([Link]):
latitude = [Link]()
longtitude = [Link]()
class Place([Link]):
...
location = EmbeddedModelField(Point)
objects = MongoDBManager()
To find all places near to your current location, 42°N | 𝜋°E, you can use this raw query:
As stated above, raw_query() returns a standard Django queryset, for which reason you can have even more fun
with raw queries:
and whatnot.
Raw Updates
raw_update() comes into play when Django MongoDB Engine’s atomic updates through $set and $inc (using
F) are not powerful enough.
The first argument is the query which describes the subset of documents the update should be executed against - as Q
object or Mongo query. The second argument is the update spec.
Consider this model:
class FancyNumbers([Link]):
foo = [Link]()
objects = MongoDBManager()
That bitwise-ORs every foo of all documents in the database with 42.
To run that update against a subset of the documents, for example against any whose foo is greater than 𝜋, use a
non-empty filter condition:
24 Chapter 1. Contents
Django MongoDB Engine, Release
PyMongo-level
[Link] is a dictionary-like object that holds all database connections – that is, for MongoDB
databases, django_mongodb_engine.[Link] instances.
These instances can be used to get the PyMongo-level Connection, Database and Collection objects.
For example, to execute a find_and_modify() command, you could use code similar to this:
Reference
Fields
This is a reference of both fields that are implemented in djangotoolbox and fields specific to MongoDB.
(In signatures, ... represents arbitrary positional and keyword arguments that are passed to [Link].
Field.)
in djangotoolbox
in django_mongodb_engine
GridFS Storage
Map/Reduce
class MapReduceableModel([Link]):
...
objects = MongoDBManager()
>>> [Link](...).map_reduce(...)
Settings
Client Settings
1.4. Reference 25
Django MongoDB Engine, Release
DATABASES = {
'default' : {
'ENGINE' : 'django_mongodb_engine',
'NAME' : 'my_database',
...
'OPTIONS' : {
'socketTimeoutMS' : 500,
...
}
}
}
All of these settings directly mirror PyMongo settings. In fact, all Django MongoDB Engine does is lower-casing
the names before passing the flags to MongoClient. For a list of possible options head over to the PyMongo
documentation on client options.
Acknowledged Operations
Use the OPERATIONS dict to specify extra flags passed to [Link], update() or remove() (and
thus included in the write concern):
'OPTIONS' : {
'OPERATIONS' : {'w' : 3},
...
}
'OPTIONS' : {
'OPERATIONS' : {
'save' : {'w' : 3},
'update' : {},
'delete' : {'j' : True}
},
...
}
Note: This operations map to the Django operations save, update and delete (not to MongoDB operations). This is
because Django abstracts “insert vs. update” into save.
A full list of write concern flags may be found in the MongoDB documentation.
Model Options
In addition to Django’s default Meta options, Django MongoDB Engine supports various options specific to MongoDB
through a special class MongoMeta.
class FooModel([Link]):
...
class MongoMeta:
# Mongo options here
...
26 Chapter 1. Contents
Django MongoDB Engine, Release
Indexes
Django MongoDB Engine already understands the standard db_index and unique_together options and gen-
erates the corresponding MongoDB indexes on syncdb.
To make use of other index features, like multi-key indexes and Geospatial Indexing, additional indexes can be speci-
fied using the indexes setting.
class Club([Link]):
location = ListField()
rating = [Link]()
admission = [Link]()
...
class MongoMeta:
indexes = [
[('rating', -1)],
[('rating', -1), ('admission', 1)],
{'fields': [('location', '2d')], 'min': -42, 'max': 42},
]
Capped Collections
Use the capped option and collection_size (and/or collection_max) to limit a collection in size (and/or
document count), new documents replacing old ones after reaching one of the limit sets.
For example, a logging collection fixed to 50MiB could be defined as follows:
class LogEntry([Link]):
timestamp = [Link]()
message = [Link]()
...
class MongoMeta:
capped = True
collection_size = 50*1024*1024
Warning: These APIs are available for MongoDB only, so using any of these features breaks portability to other
non-relational databases (Google App Engine, Cassandra, Redis, ...). For the sake of portability you should try to
avoid database-specific features whenever possible.
Lower-Level API
class FooModel([Link]):
1.4. Reference 27
Django MongoDB Engine, Release
...
objects = MongoDBManager()
>>> [Link].raw_query(...)
>>> [Link].raw_update(...)
Troubleshooting
This page is going to be a collection of common issues Django MongoDB Engine users faced. Please help grow this
collection – tell us about your troubles!
SITE_ID issues
˓→string.
This means that your SITE_ID setting (What’s SITE_ID?!) is incorrect – it is set to “1” but the site object that has
automatically been created has an ObjectId primary key.
If you add 'django_mongodb_engine' to your list of INSTALLED_APPS, you can use the tellsiteid
command to get the default site’s ObjectId and update your SITE_ID setting accordingly:
$ ./[Link] tellsiteid
The default site's ID is u'deafbeefdeadbeef00000000'. To use the sites framework, add
˓→this line to [Link]:
SITE_ID=u'deafbeefdeadbeef00000000'
DatabaseError at /admin/auth/user/deafbeefdeadbeef00000000/
[...] This query is not supported by the database.
This happens because Django tries to execute JOINs in order to display a list of groups/permissions in the user edit
form.
To workaround this problem, add 'djangotoolbox' to your INSTALLED_APPS which makes the Django admin
skip the groups and permissions widgets.
See [Link]
Meta
Changelog
28 Chapter 1. Contents
Django MongoDB Engine, Release
Major changes
Minor changes/fixes
1.6. Meta 29
Django MongoDB Engine, Release
• OR query support
• Support for DateTimeField and friends
• Support for atomic updates using F
• EmbeddedModelField has been merged into djangotoolbox. For legacy data records in your setup, you can
use the LegacyEmbeddedModelField.
• Support for raw queries and raw updates
• Aggregation support
• Map/Reduce support
• ListField, SetListField, DictField and GenericField have been merged into djangotoolbox
• Added an EmbeddedModelField to store arbitrary model instances as MongoDB embedded ob-
jects/subobjects.
• Internal Refactorings
How to Contribute
We’d love to see you getting involved in Django MongoDB Engine’s development!
Here are some ideas on how you can help evolve this project:
• Send us feedback! Tell us about your good or bad experiences with Django MongoDB Engine – what did you
like and what should be improved?
• Blog/write about using Django with MongoDB and let us know about your work.
• Help solving other people’s problems on the mailing list.
• Fix and improve this documentation. Since none of the Django MongoDB Engine developers are native speak-
ers, these documents are probably full of typos and weird, ungrammatical phrasings. Also, if you’re missing
something from this documentation, please tell us!
30 Chapter 1. Contents
Django MongoDB Engine, Release
Mailing List
Our mailing list, django-non-relational@[Link], is the right place for general feedback,
discussion and support.
Development
Bug Reports
Patches
The most comfortable way to get your changes into Django MongoDB Engine is to use GitHub’s pull requests. It’s
perfectly fine, however, to send regular patches to the mailing list.
Authors
Contributions by
1.6. Meta 31
Django MongoDB Engine, Release
32 Chapter 1. Contents