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 : |
|
---|
See general MongoDB documentation
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
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 : |
|
---|
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.
See general MongoDB documentation
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
See general MongoDB documentation
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.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 : |
|
---|
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.
Evaluate a JavaScript expression in MongoDB.
Useful if you need to touch a lot of data lightly; in such a scenario the network transfer of the data could be a bottleneck. The code argument must be a JavaScript function. Additional positional arguments will be passed to that function when it is run on the server.
Raises TypeError if code is not an instance of basestring (str in python 3) or Code. Raises OperationFailure if the eval fails. Returns the result of the evaluation.
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 : |
|
---|
All incoming SON copying manipulators installed on this instance.
New in version 2.0.
All incoming SON manipulators installed on this instance.
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.
Deauthorize use of this database for this client instance.
List all outgoing SON copying manipulators installed on this instance.
New in version 2.0.
List all outgoing SON manipulators installed on this instance.
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.
See general MongoDB documentation
Get the database’s current profiling level.
Returns one of (OFF, SLOW_ONLY, ALL).
See general MongoDB documentation
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).
See general MongoDB documentation
A 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.
With MongoDB < 1.9 the result dict will include a result key with a string value that represents the validation results. With MongoDB >= 1.9 the result key no longer exists and the results are split into individual fields in the result dict.
Parameters : |
|
---|
Get a system js helper for the database database.
An instance of SystemJS can be created with an instance of Database through Database.system_js, manual instantiation of this class should not be necessary.
SystemJS instances allow for easy manipulation and access to server-side JavaScript:
>>> db.system_js.add1 = "function (x) { return x + 1; }"
>>> db.system.js.find({"_id": "add1"}).count()
1
>>> db.system_js.add1(5)
6.0
>>> del db.system_js.add1
>>> db.system.js.find({"_id": "add1"}).count()
0
Get a list of the names of the functions stored in this database.