本文整理匯總了Python中pymongo.topology.Topology.get_direct_or_primary方法的典型用法代碼示例。如果您正苦於以下問題:Python Topology.get_direct_or_primary方法的具體用法?Python Topology.get_direct_or_primary怎麽用?Python Topology.get_direct_or_primary使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pymongo.topology.Topology
的用法示例。
在下文中一共展示了Topology.get_direct_or_primary方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: MongoClient
# 需要導入模塊: from pymongo.topology import Topology [as 別名]
# 或者: from pymongo.topology.Topology import get_direct_or_primary [as 別名]
#.........這裏部分代碼省略.........
return
if index_name in self.__index_cache[database_name][collection_name]:
del self.__index_cache[database_name][collection_name][index_name]
def _server_property(self, attr_name, default=None):
"""An attribute of the current server's description.
Returns "default" while there is no current server, primary, or mongos.
Not threadsafe if used multiple times in a single method, since
the server may change. In such cases, store a local reference to a
ServerDescription first, then use its properties.
"""
try:
server = self._topology.select_server(
writable_server_selector, server_selection_timeout=0)
return getattr(server.description, attr_name)
except ConnectionFailure:
return default
@property
def address(self):
"""(host, port) of the current standalone, primary, or mongos, or None.
Accessing :attr:`address` raises :exc:`~.errors.InvalidOperation` if
the client is load-balancing among mongoses, since there is no single
address. Use :attr:`nodes` instead.
.. versionadded:: 3.0
"""
try:
return self._topology.get_direct_or_primary()
except InvalidOperation:
# Only one case where Topology throws InvalidOperation.
raise InvalidOperation(
'Cannot use "address" property when load balancing among'
' mongoses, use "nodes" instead.')
@property
def primary(self):
"""The (host, port) of the current primary of the replica set.
Returns ``None`` if this client is not connected to a replica set,
there is no primary, or this client was created without the
`replicaSet` option.
.. versionadded:: 3.0
MongoClient gained this property in version 3.0 when
MongoReplicaSetClient's functionality was merged in.
"""
return self._topology.get_primary()
@property
def secondaries(self):
"""The secondary members known to this client.
A sequence of (host, port) pairs. Empty if this client is not
connected to a replica set, there are no visible secondaries, or this
client was created without the `replicaSet` option.
.. versionadded:: 3.0
MongoClient gained this property in version 3.0 when
MongoReplicaSetClient's functionality was merged in.
"""