本文整理匯總了Python中pymongo.read_preferences.ReadPreference.PRIMARY屬性的典型用法代碼示例。如果您正苦於以下問題:Python ReadPreference.PRIMARY屬性的具體用法?Python ReadPreference.PRIMARY怎麽用?Python ReadPreference.PRIMARY使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類pymongo.read_preferences.ReadPreference
的用法示例。
在下文中一共展示了ReadPreference.PRIMARY屬性的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _list_collections
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def _list_collections(self, sock_info, slave_okay, criteria=None):
"""Internal listCollections helper."""
criteria = criteria or {}
cmd = SON([("listCollections", 1), ("cursor", {})])
if criteria:
cmd["filter"] = criteria
if sock_info.max_wire_version > 2:
coll = self["$cmd"]
cursor = self._command(sock_info, cmd, slave_okay)["cursor"]
return CommandCursor(coll, cursor, sock_info.address)
else:
coll = self["system.namespaces"]
res = _first_batch(sock_info, coll.database.name, coll.name,
criteria, 0, slave_okay,
CodecOptions(), ReadPreference.PRIMARY, cmd,
self.client._event_listeners)
data = res["data"]
cursor = {
"id": res["cursor_id"],
"firstBatch": data,
"ns": coll.full_name,
}
# Need to tell the cursor how many docs were in the first batch.
return CommandCursor(coll, cursor, sock_info.address, len(data))
示例2: current_op
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def current_op(self, include_all=False):
"""Get information on operations currently running.
:Parameters:
- `include_all` (optional): if ``True`` also list currently
idle operations in the result
"""
cmd = SON([("currentOp", 1), ("$all", include_all)])
with self.__client._socket_for_writes() as sock_info:
if sock_info.max_wire_version >= 4:
return sock_info.command("admin", cmd)
else:
spec = {"$all": True} if include_all else {}
x = helpers._first_batch(sock_info, "admin", "$cmd.sys.inprog",
spec, -1, True, self.codec_options,
ReadPreference.PRIMARY, cmd, self.client._event_listeners)
return x.get('data', [None])[0]
示例3: drop_database
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def drop_database(self, name_or_database):
"""Drop a database.
Raises :class:`TypeError` if `name_or_database` is not an instance of
:class:`basestring` (:class:`str` in python 3) or
:class:`~pymongo.database.Database`.
:Parameters:
- `name_or_database`: the name of a database to drop, or a
:class:`~pymongo.database.Database` instance representing the
database to drop
"""
name = name_or_database
if isinstance(name, database.Database):
name = name.name
if not isinstance(name, string_type):
raise TypeError("name_or_database must be an instance "
"of %s or a Database" % (string_type.__name__,))
self._purge_index(name)
self[name].command("dropDatabase",
read_preference=ReadPreference.PRIMARY)
示例4: fsync
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def fsync(self, **kwargs):
"""Flush all pending writes to datafiles.
:Parameters:
Optional parameters can be passed as keyword arguments:
- `lock`: If True lock the server to disallow writes.
- `async`: If True don't block while synchronizing.
.. warning:: `async` and `lock` can not be used together.
.. warning:: MongoDB does not support the `async` option
on Windows and will raise an exception on that
platform.
"""
self.admin.command("fsync",
read_preference=ReadPreference.PRIMARY, **kwargs)
示例5: unlock
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def unlock(self):
"""Unlock a previously locked server.
"""
cmd = {"fsyncUnlock": 1}
with self._socket_for_writes() as sock_info:
if sock_info.max_wire_version >= 4:
try:
sock_info.command("admin", cmd)
except OperationFailure as exc:
# Ignore "DB not locked" to replicate old behavior
if exc.code != 125:
raise
else:
helpers._first_batch(sock_info, "admin", "$cmd.sys.unlock",
{}, -1, True, self.codec_options,
ReadPreference.PRIMARY, cmd, self._event_listeners)
示例6: __create_index
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def __create_index(self, keys, index_options):
"""Internal create index helper.
:Parameters:
- `keys`: a list of tuples [(key, type), (key, type), ...]
- `index_options`: a dict of index options.
"""
index_doc = helpers._index_document(keys)
index = {"key": index_doc}
index.update(index_options)
with self._socket_for_writes() as sock_info:
cmd = SON([('createIndexes', self.name), ('indexes', [index])])
try:
self._command(
sock_info, cmd, read_preference=ReadPreference.PRIMARY)
except OperationFailure as exc:
if exc.code in common.COMMAND_NOT_FOUND_CODES:
index["ns"] = self.__full_name
wcn = (self.write_concern if
self.write_concern.acknowledged else WriteConcern())
self.__database.system.indexes._insert(
sock_info, index, True, False, False, wcn)
else:
raise
示例7: connect
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def connect(self):
"""Connect to Mongo and return a new SocketInfo.
Can raise ConnectionFailure or CertificateError.
Note that the pool does not keep a reference to the socket -- you
must call return_socket() when you're done with it.
"""
sock = None
try:
sock = _configured_socket(self.address, self.opts)
if self.handshake:
ismaster = IsMaster(command(sock, 'admin', {'ismaster': 1},
False, False,
ReadPreference.PRIMARY,
DEFAULT_CODEC_OPTIONS))
else:
ismaster = None
return SocketInfo(sock, self, ismaster, self.address)
except socket.error as error:
if sock is not None:
sock.close()
_raise_connection_failure(self.address, error)
示例8: drop_database
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def drop_database(self, name_or_database):
"""Drop a database.
Raises :class:`TypeError` if `name_or_database` is not an instance of
:class:`basestring` (:class:`str` in python 3) or Database.
:Parameters:
- `name_or_database`: the name of a database to drop, or a
:class:`~pymongo.database.Database` instance representing the
database to drop
"""
name = name_or_database
if isinstance(name, database.Database):
name = name.name
if not isinstance(name, basestring):
raise TypeError("name_or_database must be an instance of "
"%s or Database" % (basestring.__name__,))
self._purge_index(name)
self[name].command("dropDatabase",
read_preference=ReadPreference.PRIMARY)
示例9: fsync
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def fsync(self, **kwargs):
"""Flush all pending writes to datafiles.
:Parameters:
Optional parameters can be passed as keyword arguments:
- `lock`: If True lock the server to disallow writes.
- `async`: If True don't block while synchronizing.
.. warning:: `async` and `lock` can not be used together.
.. warning:: MongoDB does not support the `async` option
on Windows and will raise an exception on that
platform.
.. versionadded:: 2.0
"""
self.admin.command("fsync",
read_preference=ReadPreference.PRIMARY, **kwargs)
示例10: drop_database
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def drop_database(self, name_or_database):
"""Drop a database.
Raises :class:`TypeError` if `name_or_database` is not an instance of
:class:`basestring` (:class:`str` in python 3) or Database
:Parameters:
- `name_or_database`: the name of a database to drop, or a
:class:`~pymongo.database.Database` instance representing the
database to drop
"""
name = name_or_database
if isinstance(name, database.Database):
name = name.name
if not isinstance(name, basestring):
raise TypeError("name_or_database must be an instance of "
"%s or Database" % (basestring.__name__,))
self._purge_index(name)
self[name].command("dropDatabase",
read_preference=ReadPreference.PRIMARY)
示例11: current_op
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def current_op(self, include_all: bool = False) -> dict:
"""Get information on operations currently running. Works only for
MongoDB >= 3.2
:Parameters:
- `include_all` (optional): if ``True`` also list currently
idle operations in the result
"""
connection = await self.client.get_connection()
if connection.max_wire_version < 4:
raise NotImplementedError('Only implemented from wired protocol version >= 4')
cmd = SON([('currentOp', 1), ('$all', include_all)])
return await connection.command(
'admin', cmd, ReadPreference.PRIMARY, DEFAULT_CODEC_OPTIONS
)
示例12: _command
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def _command(self, sock_info, command, slave_ok=False, value=1, check=True,
allowable_errors=None, read_preference=ReadPreference.PRIMARY,
codec_options=DEFAULT_CODEC_OPTIONS,
write_concern=None,
parse_write_concern_error=False, **kwargs):
"""Internal command helper."""
if isinstance(command, string_type):
command = SON([(command, value)])
if sock_info.max_wire_version >= 5 and write_concern:
command['writeConcern'] = write_concern.document
command.update(kwargs)
return sock_info.command(
self.__name,
command,
slave_ok,
read_preference,
codec_options,
check,
allowable_errors,
parse_write_concern_error=parse_write_concern_error)
示例13: has_readable_server
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def has_readable_server(self, read_preference=ReadPreference.PRIMARY):
"""Does this topology have any readable servers available matching the
given read preference?
:Parameters:
- `read_preference`: an instance of a read preference from
:mod:`~pymongo.read_preferences`. Defaults to
:attr:`~pymongo.read_preferences.ReadPreference.PRIMARY`.
.. note:: When connected directly to a single server this method
always returns ``True``.
.. versionadded:: 3.4
"""
common.validate_read_preference("read_preference", read_preference)
return any(self.apply_selector(read_preference, None))
示例14: reindex
# 需要導入模塊: from pymongo.read_preferences import ReadPreference [as 別名]
# 或者: from pymongo.read_preferences.ReadPreference import PRIMARY [as 別名]
def reindex(self):
"""Rebuilds all indexes on this collection.
.. warning:: reindex blocks all other operations (indexes
are built in the foreground) and will be slow for large
collections.
.. note:: The :attr:`~pymongo.collection.Collection.write_concern` of
this collection is automatically applied to this operation when using
MongoDB >= 3.4.
.. versionchanged:: 3.4
Apply this collection's write concern automatically to this operation
when connected to MongoDB >= 3.4.
"""
cmd = SON([("reIndex", self.__name)])
with self._socket_for_writes() as sock_info:
return self._command(
sock_info, cmd, read_preference=ReadPreference.PRIMARY,
write_concern=self.write_concern,
parse_write_concern_error=True)