diff --git a/docs/changelog.rst b/docs/changelog.rst index 0dcb7e94..df984059 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,7 @@ Changes in 0.10.6 - Dev ======================= - Add support for mocking MongoEngine based on mongomock. #1151 - Fixed not being able to run tests on Windows. #1153 +- Allow creation of sparse compound indexes. #1114 Changes in 0.10.5 ================= diff --git a/mongoengine/base/document.py b/mongoengine/base/document.py index 6353162a..05b3ad95 100644 --- a/mongoengine/base/document.py +++ b/mongoengine/base/document.py @@ -839,10 +839,6 @@ class BaseDocument(object): if index_list: spec['fields'] = index_list - if spec.get('sparse', False) and len(spec['fields']) > 1: - raise ValueError( - 'Sparse indexes can only have one field in them. ' - 'See https://jira.mongodb.org/browse/SERVER-2193') return spec diff --git a/tests/document/indexes.py b/tests/document/indexes.py index ccc6cf44..7ee017a6 100644 --- a/tests/document/indexes.py +++ b/tests/document/indexes.py @@ -863,6 +863,20 @@ class IndexesTest(unittest.TestCase): self.assertTrue([('provider_ids.foo', 1)] in info) self.assertTrue([('provider_ids.bar', 1)] in info) + def test_sparse_compound_indexes(self): + + class MyDoc(Document): + provider_ids = DictField() + meta = { + "indexes": [{'fields': ("provider_ids.foo", "provider_ids.bar"), + 'sparse': True}], + } + + info = MyDoc.objects._collection.index_information() + self.assertEqual([('provider_ids.foo', 1), ('provider_ids.bar', 1)], + info['provider_ids.foo_1_provider_ids.bar_1']['key']) + self.assertTrue(info['provider_ids.foo_1_provider_ids.bar_1']['sparse']) + def test_text_indexes(self): class Book(Document):