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. For example:
let db = client.database("items");
for i in 0..5 {
let db_ref = db.clone();
std::thread::spawn(move || {
let collection = db_ref.collection(&format!("coll{}", i));
// Do something with the collection
});
}
Implementations§
Source§impl Database
impl Database
Sourcepub fn selection_criteria(&self) -> Option<&SelectionCriteria>
pub fn selection_criteria(&self) -> Option<&SelectionCriteria>
Gets the read preference of the Database
.
Sourcepub fn read_concern(&self) -> Option<&ReadConcern>
pub fn read_concern(&self) -> Option<&ReadConcern>
Gets the read concern of the Database
.
Sourcepub fn write_concern(&self) -> Option<&WriteConcern>
pub fn write_concern(&self) -> Option<&WriteConcern>
Gets the write concern of the Database
.
Sourcepub fn collection(&self, name: &str) -> Collection
pub fn collection(&self, name: &str) -> Collection
Gets a handle to a collection specified by name
of the database. The Collection
options
(e.g. read preference and write concern) will default to those of the Database
.
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.
Sourcepub fn collection_with_type<T>(&self, name: &str) -> Collection<T>
pub fn collection_with_type<T>(&self, name: &str) -> Collection<T>
Gets a handle to a collection with type T
specified by name
of the database. The
Collection
options (e.g. read preference and write concern) will default to those of the
Database
.
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.
Sourcepub fn collection_with_options(
&self,
name: &str,
options: CollectionOptions,
) -> Collection
pub fn collection_with_options( &self, name: &str, options: CollectionOptions, ) -> Collection
Gets a handle to a collection specified by name
in the cluster the Client
is connected
to. Operations done with this Collection
will use the options specified by options
by
default and will otherwise default to those of the Database
.
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.
Sourcepub fn collection_with_type_and_options<T>(
&self,
name: &str,
options: CollectionOptions,
) -> Collection<T>
pub fn collection_with_type_and_options<T>( &self, name: &str, options: CollectionOptions, ) -> Collection<T>
Gets a handle to a collection with type T
specified by name
in the cluster the Client
is connected to. Operations done with this Collection
will use the options specified by
options
by default and will otherwise default to those of the Database
.
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.
Sourcepub fn drop(
&self,
options: impl Into<Option<DropDatabaseOptions>>,
) -> Result<(), Error>
pub fn drop( &self, options: impl Into<Option<DropDatabaseOptions>>, ) -> Result<(), Error>
Drops the database, deleting all data, collections, users, and indexes stored in it.
Sourcepub fn list_collections(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<ListCollectionsOptions>>,
) -> Result<Cursor, Error>
pub fn list_collections( &self, filter: impl Into<Option<Document>>, options: impl Into<Option<ListCollectionsOptions>>, ) -> Result<Cursor, Error>
Gets information about each of the collections in the database. The cursor will yield a document pertaining to each collection in the database.
Sourcepub fn list_collection_names(
&self,
filter: impl Into<Option<Document>>,
) -> Result<Vec<String>, Error>
pub fn list_collection_names( &self, filter: impl Into<Option<Document>>, ) -> Result<Vec<String>, Error>
Gets the names of the collections in the database.
Sourcepub fn create_collection(
&self,
name: &str,
options: impl Into<Option<CreateCollectionOptions>>,
) -> Result<(), Error>
pub fn create_collection( &self, name: &str, options: impl Into<Option<CreateCollectionOptions>>, ) -> Result<(), Error>
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.
Sourcepub fn run_command(
&self,
command: Document,
selection_criteria: impl Into<Option<SelectionCriteria>>,
) -> Result<Document, Error>
pub fn run_command( &self, command: Document, selection_criteria: impl Into<Option<SelectionCriteria>>, ) -> Result<Document, Error>
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.