Improvements to indexing documentation (#130)

This commit is contained in:
Ross Lawley 2013-04-29 07:38:31 +00:00
parent 6e2d2f33de
commit b0c1ec04b5
3 changed files with 28 additions and 9 deletions

View File

@ -479,6 +479,10 @@ If a dictionary is passed then the following options are available:
:attr:`unique` (Default: False)
Whether the index should be unique.
:attr:`expireAfterSeconds` (Optional)
Allows you to automatically expire data from a collection by setting the
time in seconds to expire the a field.
.. note::
Inheritance adds extra fields indices see: :ref:`document-inheritance`.
@ -512,6 +516,22 @@ point. To create a geospatial index you must prefix the field with the
],
}
Time To Live indexes
--------------------
A special index type that allows you to automatically expire data from a
collection after a given period. See the official
`ttl <http://docs.mongodb.org/manual/tutorial/expire-data/#expire-data-from-collections-by-setting-ttl>`_
documentation for more information. A common usecase might be session data::
class Session(Document):
created = DateTimeField(default=datetime.now)
meta = {
'indexes': [
{'fields': ['created'], 'expireAfterSeconds': 3600}
]
}
Ordering
========
A default ordering can be specified for your

View File

@ -8,6 +8,7 @@ import uuid
import warnings
from operator import itemgetter
import pymongo
import gridfs
from bson import Binary, DBRef, SON, ObjectId
@ -37,7 +38,6 @@ __all__ = ['StringField', 'URLField', 'EmailField', 'IntField', 'LongField',
'SequenceField', 'UUIDField']
RECURSIVE_REFERENCE_CONSTANT = 'self'
@ -1392,7 +1392,7 @@ class GeoPointField(BaseField):
.. versionadded:: 0.4
"""
_geo_index = True
_geo_index = pymongo.GEO2D
def validate(self, value):
"""Make sure that a geo-value is of type (x, y)
@ -1404,7 +1404,7 @@ class GeoPointField(BaseField):
if not len(value) == 2:
self.error('Value must be a two-dimensional point')
if (not isinstance(value[0], (float, int)) and
not isinstance(value[1], (float, int))):
not isinstance(value[1], (float, int))):
self.error('Both values in point must be float or int')

View File

@ -217,7 +217,7 @@ class IndexesTest(unittest.TestCase):
}
self.assertEqual([{'fields': [('location.point', '2d')]}],
Place._meta['index_specs'])
Place._meta['index_specs'])
Place.ensure_indexes()
info = Place._get_collection().index_information()
@ -231,8 +231,7 @@ class IndexesTest(unittest.TestCase):
location = DictField()
class Place(Document):
current = DictField(
field=EmbeddedDocumentField('EmbeddedLocation'))
current = DictField(field=EmbeddedDocumentField('EmbeddedLocation'))
meta = {
'allow_inheritance': True,
'indexes': [
@ -241,7 +240,7 @@ class IndexesTest(unittest.TestCase):
}
self.assertEqual([{'fields': [('current.location.point', '2d')]}],
Place._meta['index_specs'])
Place._meta['index_specs'])
Place.ensure_indexes()
info = Place._get_collection().index_information()
@ -264,7 +263,7 @@ class IndexesTest(unittest.TestCase):
self.assertEqual([{'fields': [('addDate', -1)], 'unique': True,
'sparse': True}],
BlogPost._meta['index_specs'])
BlogPost._meta['index_specs'])
BlogPost.drop_collection()
@ -633,7 +632,7 @@ class IndexesTest(unittest.TestCase):
list(Log.objects)
info = Log.objects._collection.index_information()
self.assertEqual(3600,
info['created_1']['expireAfterSeconds'])
info['created_1']['expireAfterSeconds'])
def test_unique_and_indexes(self):
"""Ensure that 'unique' constraints aren't overridden by