Database level operations.
The authentication mechanisms supported by PyMongo.
No database profiling.
Only profile slow operations.
Profile all operations.
Get a database by client and name.
Raises TypeError if name is not an instance of basestring (str in python 3). Raises InvalidName if name is not a valid database name.
Parameters: |
|
---|
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 Collection for attribute names with leading underscores. You must use dict-style lookups instead::
db[‘__my_collection__’]
Not:
db.__my_collection__
Get the collection_name Collection of Database db.
Raises InvalidName if an invalid collection name is used.
Note
Use dictionary style access if collection_name is an attribute of the Database class eg: db[collection_name].
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.
Add a new son manipulator to this database.
DEPRECATED - add_son_manipulator is deprecated.
Changed in version 3.0: Deprecated add_son_manipulator.
Create user name with password password.
Add a new user with permissions for this Database.
Note
Will change the password if user name already exists.
Parameters: |
|
---|
Note
The use of optional keyword arguments like userSource, otherDBRoles, or roles requires MongoDB >= 2.4.0
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
DEPRECATED: Authenticate to use this database.
Authentication lasts for the life of the underlying client instance, or until logout() is called.
Raises TypeError if (required) name, (optional) password, or (optional) source is not an instance of basestring (str in python 3).
Note
Parameters: |
|
---|
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 PyMongoError if authentication fails due to invalid credentials or configuration issues.
Get a list of all the collection names in this database.
Parameters: |
|
---|
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 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:
>>> db.command("buildinfo")
For a command where the value matters, like {collstats: collection_name} we can do:
>>> db.command("collstats", collection_name)
For commands that take additional arguments we can use kwargs. So {filemd5: object_id, root: file_root} becomes:
>>> db.command("filemd5", object_id, root=file_root)
Parameters: |
|
---|
Note
command() does not obey read_preference or codec_options. You must use the read_preference and codec_options parameters instead.
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.
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 PYTHON-500.
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
Create a new Collection 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.
Options should be passed as keyword arguments to this method. Supported options vary with MongoDB release. Some examples include:
- “size”: desired initial size for the collection (in bytes). For capped collections this size is the max size of the collection.
- “capped”: if True, this is a capped collection
- “max”: maximum number of objects if capped (optional)
See the MongoDB documentation for a full list of supported options by server version.
Parameters: |
|
---|
Changed in version 3.4: Added the collation option.
Changed in version 3.0: Added the codec_options, read_preference, and write_concern options.
Changed in version 2.2: Removed deprecated argument: options
Get information on operations currently running.
Parameters: |
|
---|
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.
Parameters: |
|
---|
Drop a collection.
Parameters: |
|
---|
Note
The write_concern of this database is automatically applied to this operation when using MongoDB >= 3.4.
Changed in version 3.4: Apply this database’s write concern automatically to this operation when connected to MongoDB >= 3.4.
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.
DEPRECATED: Evaluate a JavaScript expression in MongoDB.
Parameters: |
|
---|
Warning
the eval command is deprecated in MongoDB 3.0 and will be removed in a future server version.
Get a Collection with the given name and options.
Useful for creating a Collection with different codec options, read preference, and/or write concern from this Database.
>>> 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)
Parameters: |
|
---|
DEPRECATED: All incoming SON copying manipulators.
Changed in version 3.5: Deprecated.
New in version 2.0.
DEPRECATED: All incoming SON manipulators.
Changed in version 3.5: Deprecated.
New in version 2.0.
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.
DEPRECATED: Deauthorize use of this database.
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.
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 reset_error_history(). Returns None if no such errors have occurred.
Changed in version 2.8: Deprecated.
Returns a list containing current profiling information.
Get the database’s current profiling level.
Remove user name from this Database.
User name will no longer have permissions to access this Database.
Parameters: |
|
---|
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 the database’s profiling level.
Parameters: |
|
---|
Possible level values:
Level | Setting |
---|---|
OFF | Off. No profiling. |
SLOW_ONLY | On. Only includes slow operations. |
ALL | On. Includes all operations. |
Raises ValueError if level is not one of (OFF, SLOW_ONLY, ALL).
DEPRECATED: SystemJS helper for this Database.
See the documentation for SystemJS for more details.
Validate a collection.
Returns a dict of validation info. Raises CollectionInvalid if validation fails.
Parameters: |
|
---|