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.
This commit is contained in:
parent
e8dbd12f22
commit
ae0384df29
@ -714,11 +714,16 @@ subsequent calls to :meth:`~mongoengine.queryset.QuerySet.order_by`. ::
|
|||||||
Shard keys
|
Shard keys
|
||||||
==========
|
==========
|
||||||
|
|
||||||
If your collection is sharded, then you need to specify the shard key as a tuple,
|
If your collection is sharded by multiple keys, then you can improve shard
|
||||||
using the :attr:`shard_key` attribute of :attr:`~mongoengine.Document.meta`.
|
routing (and thus the performance of your application) by specifying the shard
|
||||||
This ensures that the shard key is sent with the query when calling the
|
key, using the :attr:`shard_key` attribute of
|
||||||
:meth:`~mongoengine.document.Document.save` or
|
:attr:`~mongoengine.Document.meta`. The shard key should be defined as a tuple.
|
||||||
:meth:`~mongoengine.document.Document.update` method on an existing
|
|
||||||
|
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:`~mongoengine.Document` instance::
|
||||||
|
|
||||||
class LogEntry(Document):
|
class LogEntry(Document):
|
||||||
@ -728,7 +733,8 @@ This ensures that the shard key is sent with the query when calling the
|
|||||||
data = StringField()
|
data = StringField()
|
||||||
|
|
||||||
meta = {
|
meta = {
|
||||||
'shard_key': ('machine', 'timestamp',)
|
'shard_key': ('machine', 'timestamp'),
|
||||||
|
'indexes': ('machine', 'timestamp'),
|
||||||
}
|
}
|
||||||
|
|
||||||
.. _document-inheritance:
|
.. _document-inheritance:
|
||||||
|
@ -544,7 +544,7 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def _qs(self):
|
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'):
|
if not hasattr(self, '__objects'):
|
||||||
self.__objects = QuerySet(self, self._get_collection())
|
self.__objects = QuerySet(self, self._get_collection())
|
||||||
return self.__objects
|
return self.__objects
|
||||||
@ -552,9 +552,11 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)):
|
|||||||
@property
|
@property
|
||||||
def _object_key(self):
|
def _object_key(self):
|
||||||
"""Get the query dict that can be used to fetch this object from
|
"""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
|
the database.
|
||||||
case of a sharded collection with a compound shard key, it can
|
|
||||||
contain a more complex query.
|
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}
|
select_dict = {'pk': self.pk}
|
||||||
shard_key = self.__class__._meta.get('shard_key', tuple())
|
shard_key = self.__class__._meta.get('shard_key', tuple())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user