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

impl Database[src]

pub fn name(&self) -> &str[src]

Gets the name of the Database.

pub fn selection_criteria(&self) -> Option<&SelectionCriteria>[src]

Gets the read preference of the Database.

pub fn read_concern(&self) -> Option<&ReadConcern>[src]

Gets the read concern of the Database.

pub fn write_concern(&self) -> Option<&WriteConcern>[src]

Gets the write concern of the Database.

pub fn collection<T>(&self, name: &str) -> Collection<T> where
    T: Serialize + DeserializeOwned + Unpin + Debug
[src]

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.

pub fn collection_with_options<T>(
    &self,
    name: &str,
    options: CollectionOptions
) -> Collection<T> where
    T: Serialize + DeserializeOwned + Unpin + Debug
[src]

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.

pub async fn drop(
    &self,
    options: impl Into<Option<DropDatabaseOptions>>
) -> Result<()>
[src]

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<()>
[src]

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>>
[src]

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>>
[src]

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>>
[src]

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>>
[src]

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<()>
[src]

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<()>
[src]

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>
[src]

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>
[src]

Runs a database-level command using the provided ClientSession.

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 aggregate(
    &self,
    pipeline: impl IntoIterator<Item = Document>,
    options: impl Into<Option<AggregateOptions>>
) -> Result<Cursor<Document>>
[src]

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>>
[src]

Runs an aggregation operation with the provided ClientSession.

See the documentation here for more information on aggregations.

Trait Implementations

impl Clone for Database[src]

fn clone(&self) -> Database[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for Database[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl !RefUnwindSafe for Database

impl Send for Database

impl Sync for Database

impl Unpin for Database

impl !UnwindSafe for Database

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V