Struct mongodb::Database

source ·
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§

source§

impl Database

source

pub fn name(&self) -> &str

Gets the name of the Database.

source

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

Gets the read preference of the Database.

source

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

Gets the read concern of the Database.

source

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

Gets the write concern of the Database.

source

pub fn collection<T>(&self, name: &str) -> Collection<T>

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.

source

pub fn collection_with_options<T>( &self, name: &str, options: CollectionOptions ) -> Collection<T>

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.

source

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

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

source

pub async fn drop_with_session( &self, options: impl Into<Option<DropDatabaseOptions>>, session: &mut ClientSession ) -> Result<()>

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

source

pub async fn list_collections( &self, filter: impl Into<Option<Document>>, options: impl Into<Option<ListCollectionsOptions>> ) -> Result<Cursor<CollectionSpecification>>

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

source

pub async fn list_collections_with_session( &self, filter: impl Into<Option<Document>>, options: impl Into<Option<ListCollectionsOptions>>, session: &mut ClientSession ) -> Result<SessionCursor<CollectionSpecification>>

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.

source

pub async fn list_collection_names( &self, filter: impl Into<Option<Document>> ) -> Result<Vec<String>>

Gets the names of the collections in the database.

source

pub async fn list_collection_names_with_session( &self, filter: impl Into<Option<Document>>, session: &mut ClientSession ) -> Result<Vec<String>>

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

source

pub async fn create_collection( &self, name: impl AsRef<str>, options: impl Into<Option<CreateCollectionOptions>> ) -> Result<()>

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.

source

pub async fn create_collection_with_session( &self, name: impl AsRef<str>, options: impl Into<Option<CreateCollectionOptions>>, session: &mut ClientSession ) -> Result<()>

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.

source

pub async fn run_command( &self, command: Document, selection_criteria: impl Into<Option<SelectionCriteria>> ) -> Result<Document>

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.

source

pub async fn run_command_with_session( &self, command: Document, selection_criteria: impl Into<Option<SelectionCriteria>>, session: &mut ClientSession ) -> Result<Document>

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.

source

pub async fn aggregate( &self, pipeline: impl IntoIterator<Item = Document>, options: impl Into<Option<AggregateOptions>> ) -> Result<Cursor<Document>>

Runs an aggregation operation.

See the documentation here for more information on aggregations.

source

pub async fn aggregate_with_session( &self, pipeline: impl IntoIterator<Item = Document>, options: impl Into<Option<AggregateOptions>>, session: &mut ClientSession ) -> Result<SessionCursor<Document>>

Runs an aggregation operation with the provided ClientSession.

See the documentation here for more information on aggregations.

source

pub async fn watch( &self, pipeline: impl IntoIterator<Item = Document>, options: impl Into<Option<ChangeStreamOptions>> ) -> Result<ChangeStream<ChangeStreamEvent<Document>>>

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.

source

pub async fn watch_with_session( &self, pipeline: impl IntoIterator<Item = Document>, options: impl Into<Option<ChangeStreamOptions>>, session: &mut ClientSession ) -> Result<SessionChangeStream<ChangeStreamEvent<Document>>>

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

source

pub fn gridfs_bucket( &self, options: impl Into<Option<GridFsBucketOptions>> ) -> GridFsBucket

Creates a new GridFsBucket in the database with the given options.

Trait Implementations§

source§

impl Clone for Database

source§

fn clone(&self) -> Database

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Database

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

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

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

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

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more