Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

This is the main entry point for the synchronous API. A Client is used to connect to a MongoDB cluster. By default, it will monitor the topology of the cluster, keeping track of any changes, such as servers being added or removed.

Client is a wrapper around the asynchronous mongodb::Client, and it starts up an async-std runtime internally to run that wrapped client on.

Client uses std::sync::Arc internally, so it can safely be shared across threads. For example:

let client = Client::with_uri_str("mongodb://example.com")?;

for i in 0..5 {
    let client_ref = client.clone();

    std::thread::spawn(move || {
        let collection = client_ref.database("items").collection(&format!("coll{}", i));

        // Do something with the collection
    });
}

§TCP Keepalive

TCP keepalive is enabled by default with tcp_keepalive_time set to 120 seconds. The driver does not set tcp_keepalive_intvl. See the MongoDB Diagnostics FAQ keepalive section for instructions on setting these values at the system level.

Implementations§

Source§

impl Client

Source

pub fn with_uri_str(uri: &str) -> Result<Client, Error>

Creates a new Client connected to the cluster specified by uri. uri must be a valid MongoDB connection string.

See the documentation on ClientOptions::parse for more details.

Source

pub fn with_options(options: ClientOptions) -> Result<Client, Error>

Creates a new Client connected to the cluster specified by options.

Source

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

Gets the default selection criteria the Client uses for operations..

Source

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

Gets the default read concern the Client uses for operations.

Source

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

Gets the default write concern the Client uses for operations.

Source

pub fn database(&self, name: &str) -> Database

Gets a handle to a database specified by name in the cluster the Client is connected to. The Database options (e.g. read preference and write concern) will default to those of the Client.

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 database_with_options( &self, name: &str, options: DatabaseOptions, ) -> Database

Gets a handle to a database specified by name in the cluster the Client is connected to. Operations done with this Database will use the options specified by options by default and will otherwise default to those of the Client.

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 list_databases( &self, filter: impl Into<Option<Document>>, options: impl Into<Option<ListDatabasesOptions>>, ) -> Result<Vec<Document>, Error>

Gets information about each database present in the cluster the Client is connected to.

Source

pub fn list_database_names( &self, filter: impl Into<Option<Document>>, options: impl Into<Option<ListDatabasesOptions>>, ) -> Result<Vec<String>, Error>

Gets the names of the databases present in the cluster the Client is connected to.

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a duplicate 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 Client

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,