From d3962c4f7dde7fe1e8d4a89c0f0df222e8583b5c Mon Sep 17 00:00:00 2001 From: Robert Kajic Date: Tue, 31 Jan 2012 22:31:24 +0100 Subject: [PATCH] Added support for creating a geo2d index by prefixing the field name with a * --- mongoengine/queryset.py | 10 ++++++---- tests/document.py | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index a6626855..56ec5238 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -399,12 +399,14 @@ class QuerySet(object): index_list = [] use_types = doc_cls._meta.get('allow_inheritance', True) for key in spec['fields']: - # Get direction from + or - + # Get ASCENDING direction from +, DESCENDING from -, and GEO2D from * direction = pymongo.ASCENDING if key.startswith("-"): direction = pymongo.DESCENDING - if key.startswith(("+", "-")): - key = key[1:] + elif key.startswith("*"): + direction = pymongo.GEO2D + if key.startswith(("+", "-", "*")): + key = key[1:] # Use real field name, do it manually because we need field # objects for the next part (list field checking) @@ -421,7 +423,7 @@ class QuerySet(object): # If _types is being used, prepend it to every specified index index_types = doc_cls._meta.get('index_types', True) allow_inheritance = doc_cls._meta.get('allow_inheritance') - if spec.get('types', index_types) and allow_inheritance and use_types: + if spec.get('types', index_types) and allow_inheritance and use_types and direction is not pymongo.GEO2D: index_list.insert(0, ('_types', 1)) spec['fields'] = index_list diff --git a/tests/document.py b/tests/document.py index 95f37748..0a267109 100644 --- a/tests/document.py +++ b/tests/document.py @@ -658,6 +658,26 @@ class DocumentTest(unittest.TestCase): BlogPost.drop_collection() + def test_explicit_geo2d_index(self): + """Ensure that geo2d indexes work when created via meta[indexes] + """ + class Place(Document): + location = DictField() + meta = { + 'indexes': [ + '*location.point', + ], + } + Place.drop_collection() + + info = Place.objects._collection.index_information() + # Indexes are lazy so use list() to perform query + list(Place.objects) + info = Place.objects._collection.index_information() + info = [value['key'] for key, value in info.iteritems()] + + self.assertTrue([('location.point', '2d')] in info) + def test_dictionary_indexes(self): """Ensure that indexes are used when meta[indexes] contains dictionaries instead of lists.