database – Database level operations

    pymongo.auth.MECHANISMS = frozenset({‘MONGODB-CR’, ‘PLAIN’, ‘MONGODB-AWS’, ‘SCRAM-SHA-256’, ‘MONGODB-X509’, ‘DEFAULT’, ‘SCRAM-SHA-1’, ‘GSSAPI’})

    The authentication mechanisms supported by PyMongo.

    pymongo.OFF = 0

    DEPRECATED - No database profiling.

    DEPRECATED - OFF is deprecated and will be removed in PyMongo 4.0. Instead, specify this profiling level using the numeric value 0. See

    Changed in version 3.12: Deprecated

    pymongo.SLOW_ONLY = 1

    DEPRECATED - Only profile slow operations.

    DEPRECATED - SLOW_ONLY is deprecated and will be removed in PyMongo 4.0. Instead, specify this profiling level using the numeric value 1. See

    Changed in version 3.12: Deprecated

    pymongo.ALL = 2

    DEPRECATED - Profile all operations.

    DEPRECATED - ALL is deprecated and will be removed in PyMongo 4.0. Instead, specify this profiling level using the numeric value 2. See

    Changed in version 3.12: Deprecated

    class pymongo.database.Database(client, name, codec_options=None, read_preference=None, write_concern=None, read_concern=None)

    Get a database by client and name.

    Raises TypeError if name is not an instance of basestring ( in python 3). Raises InvalidName if name is not a valid database name.

    See also

    The MongoDB documentation on .

    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. Database no longer returns an instance of for attribute names with leading underscores. You must use dict-style lookups instead::

    Not:

    • db[collection_name] || db.collection_name

      Get the collection_name Collection of db.

      Raises InvalidName if an invalid collection name is used.

      Note

      Use dictionary style access if collection_name is an attribute of the class eg: db[collection_name].

    • codec_options

      Read only access to the CodecOptions of this instance.

    • read_preference

      Read only access to the read preference of this instance.

      Changed in version 3.0: The attribute is now read only.

    • write_concern

      Read only access to the WriteConcern of this instance.

      Changed in version 3.0: The attribute is now read only.

    • read_concern

      Read only access to the ReadConcern of this instance.

      New in version 3.2.

    • add_son_manipulator(manipulator)

      Add a new son manipulator to this database.

      DEPRECATED - add_son_manipulator is deprecated.

      Changed in version 3.0: Deprecated add_son_manipulator.

    • add_user(name, password=None, read_only=None, session=None, \*kwargs*)

      DEPRECATED: Create user name with password password.

      Add a new user with permissions for this .

      Note

      Will change the password if user name already exists.

      Note

      add_user is deprecated and will be removed in PyMongo 4.0. Starting with MongoDB 2.6 user management is handled with four database commands, createUser, , updateUser, and .

      To create a user:

      To create a read-only user:

      1. db.command("createUser", "user", pwd="password", roles=["read"])

      To change a password:

      1. db.command("updateUser", "user", pwd="newpassword")

      Or change roles:

      1. db.command("updateUser", "user", roles=["readWrite"])

      Warning

      Never create or modify users over an insecure network without the use of TLS. See TLS/SSL and PyMongo for more information.

      Parameters:
      • name: the name of the user to create
      • password (optional): the password of the user to create. Can not be used with the userSource argument.
      • read_only (optional): if True the user will be read only
      • **kwargs (optional): optional fields for the user document (e.g. userSource, otherDBRoles, or roles). See for more information.
      • session (optional): a ClientSession.

      Changed in version 3.7: Added support for SCRAM-SHA-256 users with MongoDB 4.0 and later.

      Changed in version 3.6: Added session parameter. Deprecated add_user.

      Changed in version 2.5: Added kwargs support for optional fields introduced in MongoDB 2.4

      Changed in version 2.2: Added support for read only users

    • aggregate(pipeline, session=None, \*kwargs*)

      Perform a database-level aggregation.

      See the documentation for a list of stages that are supported.

      Introduced in MongoDB 3.6.

      1. # Lists all operations currently running on the server.
      2. with client.admin.aggregate([{"$currentOp": {}}]) as cursor:
      3. for operation in cursor:
      4. print(operation)

      The aggregate() method obeys the of this Database, except when $out or $merge are used, in which case is used.

      Note

      This method does not support the ‘explain’ option. Please use command() instead.

      Note

      The of this collection is automatically applied to this operation.

      Parameters:
      • pipeline: a list of aggregation pipeline stages
      • session (optional): a ClientSession.
      • **kwargs (optional): extra parameters.

      All optional aggregate command parameters should be passed as keyword arguments to this method. Valid options include, but are not limited to:

      Returns:A over the result set.

      New in version 3.9.

    • authenticate(name=None, password=None, source=None, mechanism=’DEFAULT’, \*kwargs*)

      DEPRECATED: Authenticate to use this database.

      Warning

      Starting in MongoDB 3.6, calling authenticate() invalidates all existing cursors. It may also leave logical sessions open on the server for up to 30 minutes until they time out.

      Authentication lasts for the life of the underlying client instance, or until is called.

      Raises TypeError if (required) name, (optional) password, or (optional) source is not an instance of basestring ( in python 3).

      Note

      • This method authenticates the current connection, and will also cause all new socket connections in the underlying client instance to be authenticated automatically.
      • Authenticating more than once on the same database with different credentials is not supported. You must call logout() before authenticating with new credentials.
      • When sharing a client instance between multiple threads, all threads will share the authentication. If you need different authentication profiles for different purposes you must use distinct client instances.
      Parameters:
      • name: the name of the user to authenticate. Optional when mechanism is MONGODB-X509 and the MongoDB server version is >= 3.4.
      • password (optional): the password of the user to authenticate. Not used with GSSAPI or MONGODB-X509 authentication.
      • source (optional): the database to authenticate on. If not specified the current database is used.
      • mechanism (optional): See for options. If no mechanism is specified, PyMongo automatically uses MONGODB-CR when connected to a pre-3.0 version of MongoDB, SCRAM-SHA-1 when connected to MongoDB 3.0 through 3.6, and negotiates the mechanism to use (SCRAM-SHA-1 or SCRAM-SHA-256) when connected to MongoDB 4.0+.
      • authMechanismProperties (optional): Used to specify authentication mechanism specific options. To specify the service name for GSSAPI authentication pass authMechanismProperties=’SERVICE_NAME:<service name>’. To specify the session token for MONGODB-AWS authentication pass authMechanismProperties=’AWS_SESSION_TOKEN:<session token>’.

      Changed in version 3.7: Added support for SCRAM-SHA-256 with MongoDB 4.0 and later.

      Changed in version 3.5: Deprecated. Authenticating multiple users conflicts with support for logical sessions in MongoDB 3.6. To authenticate as multiple users, create multiple instances of MongoClient.

      New in version 2.8: Use SCRAM-SHA-1 with MongoDB 3.0 and later.

      Changed in version 2.5: Added the source and mechanism parameters. authenticate() now raises a subclass of if authentication fails due to invalid credentials or configuration issues.

      See also

      The MongoDB documentation on authenticate.

    • client

      The client instance for this .

    • DEPRECATED: Get a list of all the collection names in this database.

      Parameters:
      • include_system_collections (optional): if False list will not include system collections (e.g system.indexes)
      • session (optional): a ClientSession.

      Changed in version 3.7: Deprecated. Use instead.

      Changed in version 3.6: Added session parameter.

    • command(command, value=1, check=True, allowable_errors=None, read_preference=None, codec_options=CodecOptions(document_class=dict, tz_aware=False, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler=’strict’, tzinfo=None, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None)), session=None, \*kwargs*)

      Issue a MongoDB command.

      Send command command to the database and return the response. If command is an instance of basestring (str in python 3) then the command {command: value} will be sent. Otherwise, command must be an instance of 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:

      1. >>> db.command("buildinfo")

      For a command where the value matters, like {collstats: collection_name} we can do:

      For commands that take additional arguments we can use kwargs. So {filemd5: object_id, root: file_root} becomes:

      1. >>> db.command("filemd5", object_id, root=file_root)
      Parameters:
      • 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 use an instance of SON or a string and kwargs instead of a Python dict.

      • value (optional): value to use for the command verb when command is passed as a string

      • check (optional): check the response for errors, raising if there are any

      • allowable_errors: if check is , error messages in this list will be ignored by error-checking

      • read_preference (optional): 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 .

      • codec_options: A CodecOptions instance.

      • session (optional): A .

      • **kwargs (optional): additional keyword arguments will be added to the command document before it is sent

      Note

      command() does not obey this Database’s or codec_options. You must use the read_preference and codec_options parameters instead.

      Note

      does not apply any custom TypeDecoders when decoding the command response.

      Note

      If this client has been configured to use MongoDB Versioned API (see MongoDB Versioned API), then will automactically 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 to attempt to convert from a BSON regular expression to a Python regular expression object. Added the codec_options parameter.

      Changed in version 2.7: Added compile_re option. If set to False, PyMongo represented BSON regular expressions as Regex objects instead of attempting to compile BSON regular expressions as Python native regular expressions, thus preventing errors for some incompatible patterns, see .

      Changed in version 2.3: Added tag_sets and secondary_acceptable_latency_ms options.

      Changed in version 2.2: Added support for as_class - the class you want to use for the resulting documents

      See also

      The MongoDB documentation on commands.

    • current_op(include_all=False, session=None)

      DEPRECATED: Get information on operations currently running.

      Starting with MongoDB 3.6 this helper is obsolete. The functionality provided by this helper is available in MongoDB 3.6+ using the , which can be used with aggregate(). Note that, while this helper can only return a single document limited to a 16MB result, returns a cursor avoiding that limitation.

      Users of MongoDB versions older than 3.6 can use the currentOp command directly:

      1. # MongoDB 3.2 and 3.4
      2. client.admin.command("currentOp")

      Or query the “inprog” virtual collection:

      1. # MongoDB 2.6 and 3.0
      2. client.admin["$cmd.sys.inprog"].find_one()

      Changed in version 3.9: Deprecated.

      Changed in version 3.6: Added session parameter.

    • dereference(dbref, session=None, \*kwargs*)

      Dereference a , getting the document it points to.

      Raises TypeError if dbref is not an instance of . 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.

      Parameters:
      • dbref: the reference
      • session (optional): a .
      • **kwargs (optional): any additional keyword arguments are the same as the arguments to find().

      Changed in version 3.6: Added session parameter.

    • drop_collection(name_or_collection, session=None)

      Drop a collection.

      Parameters:
      • name_or_collection: the name of a collection to drop or the collection object itself
      • session (optional): a .

      Note

      The write_concern of this database is automatically applied to this operation when using MongoDB >= 3.4.

      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.

    • error()

      DEPRECATED: Get the error if one occurred on the last operation.

      This method is obsolete: all MongoDB write operations (insert, update, remove, and so on) use the write concern w=1 and report their errors by default.

      Changed in version 2.8: Deprecated.

    • eval(code, \args*)

      DEPRECATED: Evaluate a JavaScript expression in MongoDB.

      Parameters:
      • code: string representation of JavaScript code to be evaluated
      • args (optional): additional positional arguments are passed to the code being evaluated

      Warning

      the eval command is deprecated in MongoDB 3.0 and will be removed in a future server version.

    • get_collection(name, codec_options=None, read_preference=None, write_concern=None, read_concern=None)

      Get a with the given name and options.

      Useful for creating a Collection with different codec options, read preference, and/or write concern from this .

      1. >>> db.read_preference
      2. Primary()
      3. >>> coll1 = db.test
      4. >>> coll1.read_preference
      5. Primary()
      6. >>> from pymongo import ReadPreference
      7. >>> coll2 = db.get_collection(
      8. ... 'test', read_preference=ReadPreference.SECONDARY)
      9. >>> coll2.read_preference
      10. Secondary(tag_sets=None)
      Parameters:
      • name: The name of the collection - a string.
      • codec_options (optional): An instance of CodecOptions. If None (the default) the of this Database is used.
      • read_preference (optional): The read preference to use. If None (the default) the of this Database is used. See for options.
      • write_concern (optional): An instance of WriteConcern. If None (the default) the of this Database is used.
      • read_concern (optional): An instance of . If None (the default) the read_concern of this is used.
    • incoming_copying_manipulators

      DEPRECATED: All incoming SON copying manipulators.

      Changed in version 3.5: Deprecated.

      New in version 2.0.

    • incoming_manipulators

      DEPRECATED: All incoming SON manipulators.

      Changed in version 3.5: Deprecated.

      New in version 2.0.

    • last_status()

      DEPRECATED: Get status information from the last operation.

      This method is obsolete: all MongoDB write operations (insert, update, remove, and so on) use the write concern w=1 and report their errors by default.

      Returns a SON object with status information.

      Changed in version 2.8: Deprecated.

    • list_collection_names(session=None, filter=None, \*kwargs*)

      Get a list of all the collection names in this database.

      For example, to list all non-system collections:

      1. filter = {"name": {"$regex": r"^(?!system\.)"}}
      2. db.list_collection_names(filter=filter)
      Parameters:
      • session (optional): a ClientSession.
      • filter (optional): A query document to filter the list of collections returned from the listCollections command.
      • **kwargs (optional): Optional parameters of the 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.

    • list_collections(session=None, filter=None, \*kwargs*)

      Get a cursor over the collections of this database.

      Parameters:
      • session (optional): a ClientSession.
      • **kwargs (optional): Optional parameters of the can be passed as keyword arguments to this method. The supported options differ by server version.
      Returns:

      An instance of CommandCursor.

      New in version 3.6.

    • logout()

      DEPRECATED: Deauthorize use of this database.

      Warning

      Starting in MongoDB 3.6, calling invalidates all existing cursors. It may also leave logical sessions open on the server for up to 30 minutes until they time out.

    • name

      The name of this Database.

    • outgoing_copying_manipulators

      DEPRECATED: All outgoing SON copying manipulators.

      Changed in version 3.5: Deprecated.

      New in version 2.0.

    • DEPRECATED: All outgoing SON manipulators.

      Changed in version 3.5: Deprecated.

      New in version 2.0.

    • previous_error()

      DEPRECATED: Get the most recent error on this database.

      This method is obsolete: all MongoDB write operations (insert, update, remove, and so on) use the write concern w=1 and report their errors by default.

      Only returns errors that have occurred since the last call to . Returns None if no such errors have occurred.

      Changed in version 2.8: Deprecated.

    • profiling_info(session=None)

      DEPRECATED: Returns a list containing current profiling information.

      Starting with PyMongo 3.12, this helper is obsolete. Instead, users can view the database profiler output by running find() against the system.profile collection as detailed in the documentation:

      Changed in version 3.12: Deprecated.

      Changed in version 3.6: Added session parameter.

      See also

      The MongoDB documentation on profiling.

    • profiling_level(session=None)

      DEPRECATED: Get the database’s current profiling level.

      Starting with PyMongo 3.12, this helper is obsolete. Instead, users can run the , using the command() helper to get the current profiler level. Running the with the level set to -1 returns the current profiler information without changing it:

      1. res = db.command("profile", -1)
      2. profiling_level = res["was"]

      The format of res depends on the version of MongoDB in use.

      Returns one of (OFF, , ALL).

      Parameters:
      • session (optional): a .

      Changed in version 3.12: Deprecated.

      Changed in version 3.6: Added session parameter.

      See also

      The MongoDB documentation on profiling.

    • remove_user(name, session=None)

      DEPRECATED: Remove user name from this .

      User name will no longer have permissions to access this Database.

      Note

      remove_user is deprecated and will be removed in PyMongo 4.0. Use the dropUser command instead:

      1. db.command("dropUser", "user")
      Parameters:
      • name: the name of the user to remove
      • session (optional): a .

      Changed in version 3.6: Added session parameter. Deprecated remove_user.

    • reset_error_history()

      DEPRECATED: Reset the error history of this database.

      This method is obsolete: all MongoDB write operations (insert, update, remove, and so on) use the write concern w=1 and report their errors by default.

      Calls to previous_error() will only return errors that have occurred since the most recent call to this method.

      Changed in version 2.8: Deprecated.

    • set_profiling_level(level, slow_ms=None, session=None, sample_rate=None, filter=None)

      DEPRECATED: Set the database’s profiling level.

      Starting with PyMongo 3.12, this helper is obsolete. Instead, users can directly run the , using the command() helper, e.g.:

      1. res = db.command("profile", 2, filter={"op": "query"})
      Parameters:
      • level: Specifies a profiling level, see list of possible values below.
      • slow_ms: Optionally modify the threshold for the profile to consider a query or operation. Even if the profiler is off queries slower than the slow_ms level will get written to the logs.
      • session (optional): a .
      • sample_rate (optional): The fraction of slow operations that should be profiled or logged expressed as a float between 0 and 1.
      • filter (optional): A filter expression that controls which operations are profiled and logged.

      Possible level values:

      LevelSetting
      OFFOff. No profiling.
      On. Only includes slow operations.
      ALLOn. Includes all operations.

      Raises if level is not one of (OFF, , ALL).

      Changed in version 3.12: Added the sample_rate and filter parameters. Deprecated.

      Changed in version 3.6: Added session parameter.

      See also

      The MongoDB documentation on .

    • system_js

      DEPRECATED: SystemJS helper for this .

      See the documentation for SystemJS for more details.

    • validate_collection(name_or_collection, scandata=False, full=False, session=None, background=None)

      Validate a collection.

      Returns a dict of validation info. Raises CollectionInvalid if validation fails.

      See also the MongoDB documentation on the .

      Parameters:
      • name_or_collection: A Collection object or the name of a collection to validate.
      • scandata: Do extra checks beyond checking the overall structure of the collection.
      • full: 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): a ClientSession.
      • background (optional): A boolean flag that determines whether the command runs in the background. Requires MongoDB 4.4+.

      Changed in version 3.11: Added background parameter.

      Changed in version 3.6: Added session parameter.

    • watch(pipeline=None, full_document=None, resume_after=None, max_await_time_ms=None, batch_size=None, collation=None, start_at_operation_time=None, session=None, start_after=None)

      Watch changes on this database.

      Performs an aggregation with an implicit initial $changeStream stage and returns a cursor which iterates over changes on all collections in this database.

      Introduced in MongoDB 4.0.

      1. with db.watch() as stream:
      2. for change in stream:
      3. print(change)

      The DatabaseChangeStream 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.

      1. try:
      2. with db.watch(
      3. [{'$match': {'operationType': 'insert'}}]) as stream:
      4. for insert_change in stream:
      5. print(insert_change)
      6. except pymongo.errors.PyMongoError:
      7. # The ChangeStream encountered an unrecoverable error or the
      8. # resume attempt failed to recreate the cursor.
      9. logging.error('...')

      For a precise description of the resume process see the .

      Parameters:
      • pipeline (optional): 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): The fullDocument to pass as an option to the $changeStream stage. Allowed values: ‘updateLookup’. 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.
      • resume_after (optional): 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): The maximum time in milliseconds for the server to wait for changes before responding to a getMore operation.
      • batch_size (optional): The maximum number of documents to return per batch.
      • collation (optional): The Collation to use for the aggregation.
      • start_at_operation_time (optional): If provided, the resulting change stream will only return changes that occurred at or after the specified . Requires MongoDB >= 4.0.
      • session (optional): a ClientSession.
      • start_after (optional): The same as resume_after except that start_after can resume notifications after an invalidate event. This option and resume_after are mutually exclusive.
      Returns:

      A cursor.

      Changed in version 3.9: Added the start_after parameter.

      New in version 3.7.

      See also

      The MongoDB documentation on changeStreams.

    • with_options(codec_options=None, read_preference=None, write_concern=None, read_concern=None)

      Get a clone of this database changing the specified settings.

      New in version 3.8.

    class pymongo.database.SystemJS(database)

    DEPRECATED: Get a system js helper for the database database.

    SystemJS will be removed in PyMongo 4.0.