Struct mongodb::Database [−][src]
pub struct Database { /* fields omitted */ }
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
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.Gets the name of the Database
.
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.Gets the read preference of the Database
.
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.Gets the read concern of the Database
.
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.Gets the write concern of the Database
.
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.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.
pub fn collection_with_options<T>(
&self,
name: &str,
options: CollectionOptions
) -> Collection<T>
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
pub fn collection_with_options<T>(
&self,
name: &str,
options: CollectionOptions
) -> Collection<T>
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.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.
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.Drops the database, deleting all data, collections, and indexes stored in it.
pub async fn drop_with_session(
&self,
options: impl Into<Option<DropDatabaseOptions>>,
session: &mut ClientSession
) -> Result<()>
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
pub async fn drop_with_session(
&self,
options: impl Into<Option<DropDatabaseOptions>>,
session: &mut ClientSession
) -> Result<()>
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.Drops the database, deleting all data, collections, and indexes stored in it using the
provided ClientSession
.
pub async fn list_collections(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<ListCollectionsOptions>>
) -> Result<Cursor<CollectionSpecification>>
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
pub async fn list_collections(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<ListCollectionsOptions>>
) -> Result<Cursor<CollectionSpecification>>
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.Gets information about each of the collections in the database. The cursor will yield a document pertaining to each collection in the database.
pub async fn list_collections_with_session(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<ListCollectionsOptions>>,
session: &mut ClientSession
) -> Result<SessionCursor<CollectionSpecification>>
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
pub async fn list_collections_with_session(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<ListCollectionsOptions>>,
session: &mut ClientSession
) -> Result<SessionCursor<CollectionSpecification>>
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.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.
pub async fn list_collection_names(
&self,
filter: impl Into<Option<Document>>
) -> Result<Vec<String>>
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
pub async fn list_collection_names(
&self,
filter: impl Into<Option<Document>>
) -> Result<Vec<String>>
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.Gets the names of the collections in the database.
pub async fn list_collection_names_with_session(
&self,
filter: impl Into<Option<Document>>,
session: &mut ClientSession
) -> Result<Vec<String>>
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
pub async fn list_collection_names_with_session(
&self,
filter: impl Into<Option<Document>>,
session: &mut ClientSession
) -> Result<Vec<String>>
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.Gets the names of the collections in the database using the provided ClientSession
.
pub async fn create_collection(
&self,
name: impl AsRef<str>,
options: impl Into<Option<CreateCollectionOptions>>
) -> Result<()>
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
pub async fn create_collection(
&self,
name: impl AsRef<str>,
options: impl Into<Option<CreateCollectionOptions>>
) -> Result<()>
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.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.
pub async fn create_collection_with_session(
&self,
name: impl AsRef<str>,
options: impl Into<Option<CreateCollectionOptions>>,
session: &mut ClientSession
) -> Result<()>
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
pub async fn create_collection_with_session(
&self,
name: impl AsRef<str>,
options: impl Into<Option<CreateCollectionOptions>>,
session: &mut ClientSession
) -> Result<()>
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.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.
pub async fn run_command(
&self,
command: Document,
selection_criteria: impl Into<Option<SelectionCriteria>>
) -> Result<Document>
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
pub async fn run_command(
&self,
command: Document,
selection_criteria: impl Into<Option<SelectionCriteria>>
) -> Result<Document>
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.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.
pub async fn run_command_with_session(
&self,
command: Document,
selection_criteria: impl Into<Option<SelectionCriteria>>,
session: &mut ClientSession
) -> Result<Document>
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
pub async fn run_command_with_session(
&self,
command: Document,
selection_criteria: impl Into<Option<SelectionCriteria>>,
session: &mut ClientSession
) -> Result<Document>
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.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.
pub async fn aggregate(
&self,
pipeline: impl IntoIterator<Item = Document>,
options: impl Into<Option<AggregateOptions>>
) -> Result<Cursor<Document>>
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
pub async fn aggregate(
&self,
pipeline: impl IntoIterator<Item = Document>,
options: impl Into<Option<AggregateOptions>>
) -> Result<Cursor<Document>>
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.Runs an aggregation operation.
See the documentation here for more information on aggregations.
pub async fn aggregate_with_session(
&self,
pipeline: impl IntoIterator<Item = Document>,
options: impl Into<Option<AggregateOptions>>,
session: &mut ClientSession
) -> Result<SessionCursor<Document>>
This is supported on crate feature tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.
pub async fn aggregate_with_session(
&self,
pipeline: impl IntoIterator<Item = Document>,
options: impl Into<Option<AggregateOptions>>,
session: &mut ClientSession
) -> Result<SessionCursor<Document>>
tokio-runtime
and non-crate feature async-std-runtime
, or non-crate feature tokio-runtime
and crate feature async-std-runtime
only.Runs an aggregation operation with the provided ClientSession
.
See the documentation here for more information on aggregations.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Database
impl !UnwindSafe for Database
Blanket Implementations
Mutably borrows from an owned value. Read more