From ae0384df29bae05c9c86414a4edb45a533dd02d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20W=C3=B3jcik?= Date: Thu, 20 Jun 2019 11:25:51 +0200 Subject: [PATCH] Improve Document.meta.shard_key docs (#2099) This closes #2096. Previous documentation of the shard_key meta attribute was missing the crucial point that it really only matters if your collection is sharded over a compound index. --- docs/guide/defining-documents.rst | 18 ++++++++++++------ mongoengine/document.py | 10 ++++++---- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/docs/guide/defining-documents.rst b/docs/guide/defining-documents.rst index ae9d3b36..9dcca88c 100644 --- a/docs/guide/defining-documents.rst +++ b/docs/guide/defining-documents.rst @@ -714,11 +714,16 @@ subsequent calls to :meth:`~mongoengine.queryset.QuerySet.order_by`. :: Shard keys ========== -If your collection is sharded, then you need to specify the shard key as a tuple, -using the :attr:`shard_key` attribute of :attr:`~mongoengine.Document.meta`. -This ensures that the shard key is sent with the query when calling the -:meth:`~mongoengine.document.Document.save` or -:meth:`~mongoengine.document.Document.update` method on an existing +If your collection is sharded by multiple keys, then you can improve shard +routing (and thus the performance of your application) by specifying the shard +key, using the :attr:`shard_key` attribute of +:attr:`~mongoengine.Document.meta`. The shard key should be defined as a tuple. + +This ensures that the full shard key is sent with the query when calling +methods such as :meth:`~mongoengine.document.Document.save`, +:meth:`~mongoengine.document.Document.update`, +:meth:`~mongoengine.document.Document.modify`, or +:meth:`~mongoengine.document.Document.delete` on an existing :class:`~mongoengine.Document` instance:: class LogEntry(Document): @@ -728,7 +733,8 @@ This ensures that the shard key is sent with the query when calling the data = StringField() meta = { - 'shard_key': ('machine', 'timestamp',) + 'shard_key': ('machine', 'timestamp'), + 'indexes': ('machine', 'timestamp'), } .. _document-inheritance: diff --git a/mongoengine/document.py b/mongoengine/document.py index cc35c440..520de5bf 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -544,7 +544,7 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)): @property def _qs(self): - """Return the queryset to use for updating / reloading / deletions.""" + """Return the default queryset corresponding to this document.""" if not hasattr(self, '__objects'): self.__objects = QuerySet(self, self._get_collection()) return self.__objects @@ -552,9 +552,11 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)): @property def _object_key(self): """Get the query dict that can be used to fetch this object from - the database. Most of the time it's a simple PK lookup, but in - case of a sharded collection with a compound shard key, it can - contain a more complex query. + the database. + + Most of the time the dict is a simple PK lookup, but in case of + a sharded collection with a compound shard key, it can contain a more + complex query. """ select_dict = {'pk': self.pk} shard_key = self.__class__._meta.get('shard_key', tuple())