mongodb/
action.rs

1//! Action builder types.
2
3mod aggregate;
4mod bulk_write;
5mod client_options;
6mod count;
7mod create_collection;
8mod create_index;
9#[cfg(feature = "in-use-encryption")]
10pub mod csfle;
11mod delete;
12mod distinct;
13mod drop;
14mod drop_index;
15mod find;
16mod find_and_modify;
17pub mod gridfs;
18mod insert_many;
19mod insert_one;
20mod list_collections;
21mod list_databases;
22mod list_indexes;
23mod perf;
24mod replace_one;
25mod run_command;
26mod search_index;
27mod session;
28mod shutdown;
29pub(crate) mod transaction;
30mod update;
31mod watch;
32
33use std::{future::IntoFuture, marker::PhantomData, ops::Deref};
34
35use crate::bson::Document;
36
37pub use aggregate::Aggregate;
38pub use bulk_write::BulkWrite;
39pub use client_options::ParseConnectionString;
40pub use count::{CountDocuments, EstimatedDocumentCount};
41pub use create_collection::CreateCollection;
42pub use create_index::CreateIndex;
43pub use delete::Delete;
44pub use distinct::Distinct;
45pub use drop::{DropCollection, DropDatabase};
46pub use drop_index::DropIndex;
47pub use find::{Find, FindOne};
48pub use find_and_modify::{FindOneAndDelete, FindOneAndReplace, FindOneAndUpdate};
49pub use insert_many::InsertMany;
50pub use insert_one::InsertOne;
51pub use list_collections::ListCollections;
52pub use list_databases::ListDatabases;
53pub use list_indexes::ListIndexes;
54pub use perf::WarmConnectionPool;
55pub use replace_one::ReplaceOne;
56pub use run_command::{RunCommand, RunCursorCommand};
57pub use search_index::{CreateSearchIndex, DropSearchIndex, ListSearchIndexes, UpdateSearchIndex};
58pub use session::StartSession;
59pub use shutdown::Shutdown;
60pub use transaction::{AbortTransaction, CommitTransaction, StartTransaction};
61pub use update::Update;
62pub use watch::Watch;
63
64#[allow(missing_docs)]
65pub struct ListSpecifications;
66#[allow(missing_docs)]
67pub struct ListNames;
68
69#[allow(missing_docs)]
70pub struct ImplicitSession;
71#[allow(missing_docs)]
72pub struct ExplicitSession<'a>(&'a mut crate::ClientSession);
73
74#[allow(missing_docs)]
75pub struct Single;
76#[allow(missing_docs)]
77pub struct Multiple;
78
79use mongodb_internal_macros::{export_doc, option_setters, options_doc};
80
81pub(crate) mod private {
82    pub trait Sealed {}
83}
84
85/// A pending action to execute on the server.  The action can be configured via chained methods and
86/// executed via `await` (or `run` if using the sync client).
87pub trait Action: private::Sealed + IntoFuture {
88    /// If the value is `Some`, call the provided function on `self`.  Convenient for chained
89    /// updates with values that need to be set conditionally.  For example:
90    /// ```rust
91    /// # use mongodb::{Client, error::Result};
92    /// # use bson::Document;
93    /// use mongodb::action::Action;
94    /// async fn list_my_collections(client: &Client, filter: Option<Document>) -> Result<Vec<String>> {
95    ///     client.database("my_db")
96    ///         .list_collection_names()
97    ///         .optional(filter, |a, f| a.filter(f))
98    ///         .await
99    /// }
100    /// ```
101    fn optional<Value>(self, value: Option<Value>, f: impl FnOnce(Self, Value) -> Self) -> Self
102    where
103        Self: Sized,
104    {
105        match value {
106            Some(value) => f(self, value),
107            None => self,
108        }
109    }
110}
111
112pub(crate) use mongodb_internal_macros::{action_impl, deeplink};
113
114use crate::Collection;
115
116pub(crate) struct CollRef<'a> {
117    inner: Collection<Document>,
118    _ref: PhantomData<&'a ()>,
119}
120
121impl<'a> CollRef<'a> {
122    fn new<T: Send + Sync>(coll: &'a Collection<T>) -> Self {
123        Self {
124            inner: coll.clone_with_type(),
125            _ref: PhantomData,
126        }
127    }
128}
129
130impl Deref for CollRef<'_> {
131    type Target = Collection<Document>;
132
133    fn deref(&self) -> &Self::Target {
134        &self.inner
135    }
136}