pub struct Database { /* private fields */ }
Expand description

Database is the client-side abstraction of a MongoDB database. It can be used to perform database-level operations or to obtain handles to specific collections within the database. A Database can only be obtained through a Client by calling either Client::database or Client::database_with_options.

Database uses std::sync::Arc internally, so it can safely be shared across threads or async tasks. For example:

 
let db = client.database("items");

for i in 0..5 {
    let db_ref = db.clone();

    task::spawn(async move {
        let collection = db_ref.collection::<Document>(&format!("coll{}", i));

        // Do something with the collection
    });
}

Implementations

Gets the name of the Database.

Gets the read preference of the Database.

Gets the read concern of the Database.

Gets the write concern of the Database.

Gets a handle to a collection in this database with the provided name. The Collection options (e.g. read preference and write concern) will default to those of this Database.

For more information on how the generic parameter T is used, check out the Collection documentation.

This method does not send or receive anything across the wire to the database, so it can be used repeatedly without incurring any costs from I/O.

Gets a handle to a collection in this database with the provided name. Operations done with this Collection will use the options specified by options and will otherwise default to those of this Database.

For more information on how the generic parameter T is used, check out the Collection documentation.

This method does not send or receive anything across the wire to the database, so it can be used repeatedly without incurring any costs from I/O.

Drops the database, deleting all data, collections, and indexes stored in it.

Drops the database, deleting all data, collections, and indexes stored in it using the provided ClientSession.

Gets information about each of the collections in the database. The cursor will yield a document pertaining to each collection in the database.

Gets information about each of the collections in the database using the provided ClientSession. The cursor will yield a document pertaining to each collection in the database.

Gets the names of the collections in the database.

Gets the names of the collections in the database using the provided ClientSession.

Creates a new collection in the database with the given name and options.

Note that MongoDB creates collections implicitly when data is inserted, so this method is not needed if no special options are required.

Creates a new collection in the database with the given name and options using the provided ClientSession.

Note that MongoDB creates collections implicitly when data is inserted, so this method is not needed if no special options are required.

Runs a database-level command.

Note that no inspection is done on doc, so the command will not use the database’s default read concern or write concern. If specific read concern or write concern is desired, it must be specified manually.

Runs a database-level command using the provided ClientSession.

If the ClientSession provided is currently in a transaction, command must not specify a read concern. If this operation is the first operation in the transaction, the read concern associated with the transaction will be inherited.

Otherwise no inspection is done on command, so the command will not use the database’s default read concern or write concern. If specific read concern or write concern is desired, it must be specified manually.

Runs an aggregation operation.

See the documentation here for more information on aggregations.

Runs an aggregation operation with the provided ClientSession.

See the documentation here for more information on aggregations.

Starts a new ChangeStream that receives events for all changes in this database. The stream does not observe changes from system collections and cannot be started on “config”, “local” or “admin” databases.

See the documentation here on change streams.

Change streams require either a “majority” read concern or no read concern. Anything else will cause a server error.

Note that using a $project stage to remove any of the _id, operationType or ns fields will cause an error. The driver requires these fields to support resumability. For more information on resumability, see the documentation for ChangeStream

If the pipeline alters the structure of the returned events, the parsed type will need to be changed via ChangeStream::with_type.

Starts a new SessionChangeStream that receives events for all changes in this database using the provided ClientSession. See Database::watch for more information.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.