database
– Database level operations¶
Database level operations.
Get a database by client and name.
Raises TypeError
if name is not an instance of str
. Raises InvalidName
if name is not a valid database name.
client (AsyncMongoClient[_DocumentType]) – A AsyncMongoClient
instance.
name (str) – The database name.
codec_options (Optional[CodecOptions[_DocumentTypeArg]]) – An instance of CodecOptions
. If None
(the default) client.codec_options is used.
read_preference (Optional[_ServerMode]) – The read preference to use. If None
(the default) client.read_preference is used.
write_concern (Optional[WriteConcern]) – An instance of WriteConcern
. If None
(the default) client.write_concern is used.
read_concern (Optional[ReadConcern]) – An instance of ReadConcern
. If None
(the default) client.read_concern is used.
See also
The MongoDB documentation on databases.
Changed in version 4.0: Removed the eval, system_js, error, last_status, previous_error, reset_error_history, authenticate, logout, collection_names, current_op, add_user, remove_user, profiling_level, set_profiling_level, and profiling_info methods. See the PyMongo 4 Migration Guide.
Changed in version 3.2: Added the read_concern option.
Changed in version 3.0: Added the codec_options, read_preference, and write_concern options. AsyncDatabase
no longer returns an instance of AsyncCollection
for attribute names with leading underscores. You must use dict-style lookups instead::
db[‘__my_collection__’]
Not:
db.__my_collection__
Get the collection_name AsyncCollection
of AsyncDatabase
db.
Raises InvalidName
if an invalid collection name is used.
Note
Use dictionary style access if collection_name is an attribute of the AsyncDatabase
class eg: db[collection_name].
Get a collection of this database by name.
Raises InvalidName if an invalid collection name is used.
name (str) – the name of the collection to get
AsyncCollection[_DocumentType]
Get a collection of this database by name.
Raises InvalidName if an invalid collection name is used.
name (str) – the name of the collection to get
AsyncCollection[_DocumentType]
Read only access to the CodecOptions
of this instance.
Read only access to the read preference of this instance.
Changed in version 3.0: The read_preference
attribute is now read only.
Read only access to the WriteConcern
of this instance.
Changed in version 3.0: The write_concern
attribute is now read only.
Read only access to the ReadConcern
of this instance.
Added in version 3.2.
Perform a database-level aggregation.
See the aggregation pipeline documentation for a list of stages that are supported.
# Lists all operations currently running on the server. with client.admin.aggregate([{"$currentOp": {}}]) as cursor: for operation in cursor: print(operation)
The aggregate()
method obeys the read_preference
of this AsyncDatabase
, except when $out
or $merge
are used, in which case PRIMARY
is used.
Note
This method does not support the ‘explain’ option. Please use command()
instead.
Note
The write_concern
of this collection is automatically applied to this operation.
pipeline (_Pipeline) – a list of aggregation pipeline stages
session (Optional[AsyncClientSession]) – a AsyncClientSession
.
kwargs (Any) – extra aggregate command parameters.
AsyncCommandCursor[_DocumentType]
All optional aggregate command parameters should be passed as keyword arguments to this method. Valid options include, but are not limited to:
allowDiskUse (bool): Enables writing to temporary files. When set to True, aggregation stages can write data to the _tmp subdirectory of the –dbpath directory. The default is False.
maxTimeMS (int): The maximum amount of time to allow the operation to run in milliseconds.
batchSize (int): The maximum number of documents to return per batch. Ignored if the connected mongod or mongos does not support returning aggregate results using a cursor.
collation (optional): An instance of
Collation
.let (dict): A dict of parameter names and values. Values must be constant or closed expressions that do not reference document fields. Parameters can then be accessed as variables in an aggregate expression context (e.g.
"$$var"
). This option is only supported on MongoDB >= 5.0.
A AsyncCommandCursor
over the result set.
pipeline (_Pipeline)
session (Optional[AsyncClientSession])
kwargs (Any)
AsyncCommandCursor[_DocumentType]
Added in version 3.9.
The client instance for this AsyncDatabase
.
Issue a MongoDB command.
Send command command to the database and return the response. If command is an instance of str
then the command {command: value} will be sent. Otherwise, command must be an instance of dict
and will be sent as is.
Any additional keyword arguments will be added to the final command document before it is sent.
For example, a command like {buildinfo: 1}
can be sent using:
>>> await db.command("buildinfo") OR >>> await db.command({"buildinfo": 1})
For a command where the value matters, like {count: collection_name}
we can do:
>>> await db.command("count", collection_name) OR >>> await db.command({"count": collection_name})
For commands that take additional arguments we can use kwargs. So {count: collection_name, query: query}
becomes:
>>> await db.command("count", collection_name, query=query) OR >>> await db.command({"count": collection_name, "query": query})
command –
document representing the command to be issued, or the name of the command (for simple commands only).
Note
the order of keys in the command document is significant (the “verb” must come first), so commands which require multiple keys (e.g. findandmodify) should be done with this in mind.
value – value to use for the command verb when command is passed as a string
check – check the response for errors, raising OperationFailure
if there are any
allowable_errors – if check is True
, error messages in this list will be ignored by error-checking
read_preference – The read preference for this operation. See read_preferences
for options. If the provided session is in a transaction, defaults to the read preference configured for the transaction. Otherwise, defaults to PRIMARY
.
codec_options – A CodecOptions
instance.
session – A AsyncClientSession
.
comment – A user-provided comment to attach to this command.
kwargs – additional keyword arguments will be added to the command document before it is sent
Note
command()
does not apply any custom TypeDecoders when decoding the command response.
Note
If this client has been configured to use MongoDB Stable API (see MongoDB Stable API), then command()
will automatically add API versioning options to the given command. Explicitly adding API versioning options in the command and declaring an API version on the client is not supported.
Changed in version 3.6: Added session
parameter.
Changed in version 3.0: Removed the as_class, fields, uuid_subtype, tag_sets, and secondary_acceptable_latency_ms option. Removed compile_re option: PyMongo now always represents BSON regular expressions as Regex
objects. Use try_compile()
to attempt to convert from a BSON regular expression to a Python regular expression object. Added the codec_options
parameter.
See also
The MongoDB documentation on commands.
Create a new AsyncCollection
in this database.
Normally collection creation is automatic. This method should only be used to specify options on creation. CollectionInvalid
will be raised if the collection already exists.
name (str) – the name of the collection to create
codec_options (Optional[CodecOptions[_DocumentTypeArg]]) – An instance of CodecOptions
. If None
(the default) the codec_options
of this AsyncDatabase
is used.
read_preference (Optional[_ServerMode]) – The read preference to use. If None
(the default) the read_preference
of this AsyncDatabase
is used.
write_concern (Optional[WriteConcern]) – An instance of WriteConcern
. If None
(the default) the write_concern
of this AsyncDatabase
is used.
read_concern (Optional[ReadConcern]) – An instance of ReadConcern
. If None
(the default) the read_concern
of this AsyncDatabase
is used.
collation – An instance of Collation
.
session (Optional[AsyncClientSession]) – a AsyncClientSession
.
check_exists (Optional[bool]) – if True (the default), send a listCollections command to check if the collection already exists before creation.
kwargs (Any) – additional keyword arguments will be passed as options for the create collection command
AsyncCollection[_DocumentType]
All optional create collection command parameters should be passed as keyword arguments to this method. Valid options include, but are not limited to:
size
(int): desired initial size for the collection (in bytes). For capped collections this size is the max size of the collection.
capped
(bool): if True, this is a capped collection
max
(int): maximum number of objects if capped (optional)
timeseries
(dict): a document specifying configuration options for timeseries collections
expireAfterSeconds
(int): the number of seconds after which a document in a timeseries collection expires
validator
(dict): a document specifying validation rules or expressions for the collection
validationLevel
(str): how strictly to apply the validation rules to existing documents during an update. The default level is “strict”
validationAction
(str): whether to “error” on invalid documents (the default) or just “warn” about the violations but allow invalid documents to be inserted
indexOptionDefaults
(dict): a document specifying a default configuration for indexes when creating a collection
viewOn
(str): the name of the source collection or view from which to create the view
pipeline
(list): a list of aggregation pipeline stages
comment
(str): a user-provided comment to attach to this command. This option is only supported on MongoDB >= 4.4.
encryptedFields
(dict): (BETA) Document that describes the encrypted fields for Queryable Encryption. For example:{ "escCollection": "enxcol_.encryptedCollection.esc", "ecocCollection": "enxcol_.encryptedCollection.ecoc", "fields": [ { "path": "firstName", "keyId": Binary.from_uuid(UUID('00000000-0000-0000-0000-000000000000')), "bsonType": "string", "queries": {"queryType": "equality"} }, { "path": "ssn", "keyId": Binary.from_uuid(UUID('04104104-1041-0410-4104-104104104104')), "bsonType": "string" } ] }
clusteredIndex
(dict): Document that specifies the clustered index configuration. It must have the following form:{ // key pattern must be {_id: 1} key: <key pattern>, // required unique: <bool>, // required, must be `true` name: <string>, // optional, otherwise automatically generated v: <int>, // optional, must be `2` if provided }
changeStreamPreAndPostImages
(dict): a document with a boolean fieldenabled
for enabling pre- and post-images.
Changed in version 4.2: Added the check_exists
, clusteredIndex
, and encryptedFields
parameters.
Changed in version 3.11: This method is now supported inside multi-document transactions with MongoDB 4.4+.
Changed in version 3.6: Added session
parameter.
Changed in version 3.4: Added the collation option.
Changed in version 3.0: Added the codec_options, read_preference, and write_concern options.
Issue a MongoDB command and parse the response as a cursor.
If the response from the server does not include a cursor field, an error will be thrown.
Otherwise, behaves identically to issuing a normal MongoDB command.
command (Union[str, MutableMapping[str, Any]]) –
document representing the command to be issued, or the name of the command (for simple commands only).
Note
the order of keys in the command document is significant (the “verb” must come first), so commands which require multiple keys (e.g. findandmodify) should use an instance of SON
or a string and kwargs instead of a Python dict.
value (Any) – value to use for the command verb when command is passed as a string
read_preference (Optional[_ServerMode]) – The read preference for this operation. See read_preferences
for options. If the provided session is in a transaction, defaults to the read preference configured for the transaction. Otherwise, defaults to PRIMARY
.
codec_options (Optional[CodecOptions[_CodecDocumentType]]) – A CodecOptions
instance.
session (Optional[AsyncClientSession]) – A AsyncClientSession
.
comment (Optional[Any]) – A user-provided comment to attach to future getMores for this command.
max_await_time_ms (Optional[int]) – The number of ms to wait for more data on future getMores for this command.
kwargs (Any) – additional keyword arguments will be added to the command document before it is sent
AsyncCommandCursor[_DocumentType]
Note
command()
does not apply any custom TypeDecoders when decoding the command response.
Note
If this client has been configured to use MongoDB Stable API (see MongoDB Stable API), then command()
will automatically add API versioning options to the given command. Explicitly adding API versioning options in the command and declaring an API version on the client is not supported.
See also
The MongoDB documentation on commands.
Dereference a DBRef
, getting the document it points to.
Raises TypeError
if dbref is not an instance of DBRef
. Returns a document, or None
if the reference does not point to a valid document. Raises ValueError
if dbref has a database specified that is different from the current database.
dbref (DBRef) – the reference
session (Optional[AsyncClientSession]) – a AsyncClientSession
.
comment (Optional[Any]) – A user-provided comment to attach to this command.
kwargs (Any) – any additional keyword arguments are the same as the arguments to find()
.
Optional[_DocumentType]
Changed in version 4.1: Added comment
parameter.
Changed in version 3.6: Added session
parameter.
Drop a collection.
name_or_collection (Union[str, AsyncCollection[_DocumentTypeArg]]) – the name of a collection to drop or the collection object itself
session (Optional[AsyncClientSession]) – a AsyncClientSession
.
comment (Optional[Any]) – A user-provided comment to attach to this command.
encrypted_fields (Optional[Mapping[str, Any]]) –
(BETA) Document that describes the encrypted fields for Queryable Encryption. For example:
{ "escCollection": "enxcol_.encryptedCollection.esc", "ecocCollection": "enxcol_.encryptedCollection.ecoc", "fields": [ { "path": "firstName", "keyId": Binary.from_uuid(UUID('00000000-0000-0000-0000-000000000000')), "bsonType": "string", "queries": {"queryType": "equality"} }, { "path": "ssn", "keyId": Binary.from_uuid(UUID('04104104-1041-0410-4104-104104104104')), "bsonType": "string" } ] }
Note
The write_concern
of this database is automatically applied to this operation.
Changed in version 4.2: Added encrypted_fields
parameter.
Changed in version 4.1: Added comment
parameter.
Changed in version 3.6: Added session
parameter.
Changed in version 3.4: Apply this database’s write concern automatically to this operation when connected to MongoDB >= 3.4.
Get a AsyncCollection
with the given name and options.
Useful for creating a AsyncCollection
with different codec options, read preference, and/or write concern from this AsyncDatabase
.
>>> db.read_preference Primary() >>> coll1 = db.test >>> coll1.read_preference Primary() >>> from pymongo import ReadPreference >>> coll2 = db.get_collection( ... 'test', read_preference=ReadPreference.SECONDARY) >>> coll2.read_preference Secondary(tag_sets=None)
name (str) – The name of the collection - a string.
codec_options (Optional[CodecOptions[_DocumentTypeArg]]) – An instance of CodecOptions
. If None
(the default) the codec_options
of this AsyncDatabase
is used.
read_preference (Optional[_ServerMode]) – The read preference to use. If None
(the default) the read_preference
of this AsyncDatabase
is used. See read_preferences
for options.
write_concern (Optional[WriteConcern]) – An instance of WriteConcern
. If None
(the default) the write_concern
of this AsyncDatabase
is used.
read_concern (Optional[ReadConcern]) – An instance of ReadConcern
. If None
(the default) the read_concern
of this AsyncDatabase
is used.
AsyncCollection[_DocumentType]
Get a list of all the collection names in this database.
For example, to list all non-system collections:
filter = {"name": {"$regex": r"^(?!system\.)"}} db.list_collection_names(filter=filter)
session (Optional[AsyncClientSession]) – a AsyncClientSession
.
filter (Optional[Mapping[str, Any]]) – A query document to filter the list of collections returned from the listCollections command.
comment (Optional[Any]) – A user-provided comment to attach to this command.
kwargs (Any) – Optional parameters of the listCollections command can be passed as keyword arguments to this method. The supported options differ by server version.
Changed in version 3.8: Added the filter
and **kwargs
parameters.
Added in version 3.6.
Get a cursor over the collections of this database.
session (Optional[AsyncClientSession]) – a AsyncClientSession
.
filter (Optional[Mapping[str, Any]]) – A query document to filter the list of collections returned from the listCollections command.
comment (Optional[Any]) – A user-provided comment to attach to this command.
kwargs (Any) –
Optional parameters of the listCollections command can be passed as keyword arguments to this method. The supported options differ by server version.
An instance of AsyncCommandCursor
.
AsyncCommandCursor[MutableMapping[str, Any]]
Added in version 3.6.
The name of this AsyncDatabase
.
Validate a collection.
Returns a dict of validation info. Raises CollectionInvalid if validation fails.
See also the MongoDB documentation on the validate command.
name_or_collection (Union[str, AsyncCollection[_DocumentTypeArg]]) – An AsyncCollection object or the name of a collection to validate.
scandata (bool) – Do extra checks beyond checking the overall structure of the collection.
full (bool) – Have the server do a more thorough scan of the collection. Use with scandata for a thorough scan of the structure of the collection and the individual documents.
session (Optional[AsyncClientSession]) – a AsyncClientSession
.
background (Optional[bool]) – A boolean flag that determines whether the command runs in the background. Requires MongoDB 4.4+.
comment (Optional[Any]) – A user-provided comment to attach to this command.
Changed in version 4.1: Added comment
parameter.
Changed in version 3.11: Added background
parameter.
Changed in version 3.6: Added session
parameter.
Watch changes on this database.
Performs an aggregation with an implicit initial $changeStream
stage and returns a AsyncDatabaseChangeStream
cursor which iterates over changes on all collections in this database.
Introduced in MongoDB 4.0.
async with db.watch() as stream: async for change in stream: print(change)
The AsyncDatabaseChangeStream
iterable blocks until the next change document is returned or an error is raised. If the next()
method encounters a network error when retrieving a batch from the server, it will automatically attempt to recreate the cursor such that no change events are missed. Any error encountered during the resume attempt indicates there may be an outage and will be raised.
try: async with db.watch([{"$match": {"operationType": "insert"}}]) as stream: async for insert_change in stream: print(insert_change) except pymongo.errors.PyMongoError: # The AsyncChangeStream encountered an unrecoverable error or the # resume attempt failed to recreate the cursor. logging.error("...")
For a precise description of the resume process see the change streams specification.
pipeline (Optional[_Pipeline]) – A list of aggregation pipeline stages to append to an initial $changeStream
stage. Not all pipeline stages are valid after a $changeStream
stage, see the MongoDB documentation on change streams for the supported stages.
full_document (Optional[str]) – The fullDocument to pass as an option to the $changeStream
stage. Allowed values: ‘updateLookup’, ‘whenAvailable’, ‘required’. When set to ‘updateLookup’, the change notification for partial updates will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred.
full_document_before_change (Optional[str]) – Allowed values: ‘whenAvailable’ and ‘required’. Change events may now result in a ‘fullDocumentBeforeChange’ response field.
resume_after (Optional[Mapping[str, Any]]) – A resume token. If provided, the change stream will start returning changes that occur directly after the operation specified in the resume token. A resume token is the _id value of a change document.
max_await_time_ms (Optional[int]) – The maximum time in milliseconds for the server to wait for changes before responding to a getMore operation.
batch_size (Optional[int]) – The maximum number of documents to return per batch.
collation (Optional[_CollationIn]) – The Collation
to use for the aggregation.
start_at_operation_time (Optional[Timestamp]) – If provided, the resulting change stream will only return changes that occurred at or after the specified Timestamp
. Requires MongoDB >= 4.0.
session (Optional[AsyncClientSession]) – a AsyncClientSession
.
start_after (Optional[Mapping[str, Any]]) – The same as resume_after except that start_after can resume notifications after an invalidate event. This option and resume_after are mutually exclusive.
comment (Optional[Any]) – A user-provided comment to attach to this command.
show_expanded_events (Optional[bool]) – Include expanded events such as DDL events like dropIndexes.
A AsyncDatabaseChangeStream
cursor.
AsyncDatabaseChangeStream[_DocumentType]
Changed in version 4.3: Added show_expanded_events parameter.
Changed in version 4.2: Added full_document_before_change
parameter.
Changed in version 4.1: Added comment
parameter.
Changed in version 3.9: Added the start_after
parameter.
Added in version 3.7.
Get a clone of this database changing the specified settings.
>>> db1.read_preference Primary() >>> from pymongo.read_preferences import Secondary >>> db2 = db1.with_options(read_preference=Secondary([{'node': 'analytics'}])) >>> db1.read_preference Primary() >>> db2.read_preference Secondary(tag_sets=[{'node': 'analytics'}], max_staleness=-1, hedge=None)
codec_options – An instance of CodecOptions
. If None
(the default) the codec_options
of this AsyncCollection
is used.
read_preference – The read preference to use. If None
(the default) the read_preference
of this AsyncCollection
is used. See read_preferences
for options.
write_concern – An instance of WriteConcern
. If None
(the default) the write_concern
of this AsyncCollection
is used.
read_concern – An instance of ReadConcern
. If None
(the default) the read_concern
of this AsyncCollection
is used.
Added in version 3.8.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4