Skip to content

Commit

Permalink
Allow sparse compound indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
gmacon committed Dec 9, 2015
1 parent 31d7f70 commit 00221e3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Expand Up @@ -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
=================
Expand Down
4 changes: 0 additions & 4 deletions mongoengine/base/document.py
Expand Up @@ -839,10 +839,6 @@ def _build_index_spec(cls, spec):

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

Expand Down
14 changes: 14 additions & 0 deletions tests/document/indexes.py
Expand Up @@ -863,6 +863,20 @@ class MyDoc(Document):
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):
Expand Down

0 comments on commit 00221e3

Please sign in to comment.