pub struct Client { /* private fields */ }
Expand description
An asynchronous Client
to query mongo with.
The client uses sane defaults but these can be tweaked using the builder. To configure a
Client
, use Client::builder
.
The Client
holds a connection pool internally, so it is advised that you create once, and
reuse it.
Implementations
sourceimpl Client
impl Client
sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new Client
.
Panics
This method panics if the mongodb::Client
fails to initialise.
Use Client::builder()
if you wish to handle this failure as an Error
instead of
panicking.
sourcepub fn builder() -> ClientBuilder
pub fn builder() -> ClientBuilder
Creates a ClientInner
to configure a Client
.
This is the same as ClientBuilder::new()
.
sourcepub fn from_client<I: Into<String>>(client: Client, database: I) -> Self
pub fn from_client<I: Into<String>>(client: Client, database: I) -> Self
Constructs a new Client
using a mongodb::Client
.
sourcepub fn collection<C>(&self) -> Collection<Document> where
C: Collection,
pub fn collection<C>(&self) -> Collection<Document> where
C: Collection,
Returns the mongodb::Document
from the mongodb.
sourcepub async fn delete<C, F>(&self, filter: Option<F>) -> Result<u64, Error> where
C: AsFilter<F> + Collection,
F: Filter,
pub async fn delete<C, F>(&self, filter: Option<F>) -> Result<u64, Error> where
C: AsFilter<F> + Collection,
F: Filter,
Convenience method to delete documents from a collection using a given filter.
Errors
This method fails if the mongodb encountered an error.
sourcepub async fn delete_one<C, F>(&self, filter: F) -> Result<bool, Error> where
C: AsFilter<F> + Collection,
F: Filter,
pub async fn delete_one<C, F>(&self, filter: F) -> Result<bool, Error> where
C: AsFilter<F> + Collection,
F: Filter,
Convenience method to delete one document from a collection using a given filter.
Errors
This method fails if the mongodb encountered an error.
sourcepub async fn find<C, F>(
&self,
filter: Option<F>
) -> Result<TypedCursor<C>, Error> where
C: AsFilter<F> + Collection,
F: Filter,
pub async fn find<C, F>(
&self,
filter: Option<F>
) -> Result<TypedCursor<C>, Error> where
C: AsFilter<F> + Collection,
F: Filter,
Convenience method to find documents in a collection.
This function is mainly intended for use cases where the filter is known to return unique
hits. If you need something more complicated use find
or the FindBuilder
.
Errors
This method fails if the mongodb encountered an error.
sourcepub async fn find_one<C, F>(
&self,
filter: F
) -> Result<Option<(ObjectId, C)>, Error> where
C: AsFilter<F> + Collection,
F: Filter,
pub async fn find_one<C, F>(
&self,
filter: F
) -> Result<Option<(ObjectId, C)>, Error> where
C: AsFilter<F> + Collection,
F: Filter,
Convenience method to find a document in a collection using a given filter.
This function is mainly intended for use cases where the filter is known to return unique
hits. If you need something more complicated use find
or the FindBuilder
.
Errors
This method fails if the mongodb encountered an error, or if the found document is invalid.
sourcepub async fn insert<C>(
&self,
documents: Vec<C>
) -> Result<HashMap<usize, ObjectId>, Error> where
C: Collection,
pub async fn insert<C>(
&self,
documents: Vec<C>
) -> Result<HashMap<usize, ObjectId>, Error> where
C: Collection,
Convenience method to insert documents in a collection.
Errors
This method fails if the mongodb encountered an error, or if the found document is invalid.
sourcepub async fn insert_one<C>(&self, document: C) -> Result<ObjectId, Error> where
C: Collection,
pub async fn insert_one<C>(&self, document: C) -> Result<ObjectId, Error> where
C: Collection,
Convenience method to insert a document in a collection.
Errors
This method fails if the mongodb encountered an error, or if the found document is invalid.
sourcepub async fn replace_one<C, F>(
&self,
filter: F,
document: C
) -> Result<bool, Error> where
C: AsFilter<F> + Collection,
F: Filter,
pub async fn replace_one<C, F>(
&self,
filter: F,
document: C
) -> Result<bool, Error> where
C: AsFilter<F> + Collection,
F: Filter,
Convenience method to replace a document in a collection.
Errors
This method fails if the mongodb encountered an error.
sourcepub async fn update<C, F, U>(
&self,
filter: F,
updates: Updates<U>
) -> Result<i64, Error> where
C: AsFilter<F> + AsUpdate<U> + Collection,
F: Filter,
U: Update,
pub async fn update<C, F, U>(
&self,
filter: F,
updates: Updates<U>
) -> Result<i64, Error> where
C: AsFilter<F> + AsUpdate<U> + Collection,
F: Filter,
U: Update,
Convenience method to update documents in a collection.
Errors
This method fails if the mongodb encountered an error.
sourcepub async fn update_one<C, F, U>(
&self,
filter: F,
updates: Updates<U>
) -> Result<bool, Error> where
C: AsFilter<F> + AsUpdate<U> + Collection,
F: Filter,
U: Update,
pub async fn update_one<C, F, U>(
&self,
filter: F,
updates: Updates<U>
) -> Result<bool, Error> where
C: AsFilter<F> + AsUpdate<U> + Collection,
F: Filter,
U: Update,
Convenience method to update one document from a collection.
Errors
This method fails if the mongodb encountered an error.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
pub fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more