mongodb/action/
create_collection.rs

1use crate::bson::{Bson, Document};
2use std::time::Duration;
3
4use crate::{
5    collation::Collation,
6    concern::WriteConcern,
7    db::options::{
8        ChangeStreamPreAndPostImages,
9        ClusteredIndex,
10        IndexOptionDefaults,
11        TimeseriesOptions,
12        ValidationAction,
13        ValidationLevel,
14    },
15    options::CreateCollectionOptions,
16    ClientSession,
17    Database,
18};
19
20use super::{deeplink, export_doc, option_setters, options_doc};
21
22impl Database {
23    /// Creates a new collection in the database with the given `name`.
24    ///
25    /// Note that MongoDB creates collections implicitly when data is inserted, so this method is
26    /// not needed if no special options are required.
27    ///
28    /// `await` will return d[`Result<()>`].
29    #[deeplink]
30    #[options_doc(create_coll)]
31    pub fn create_collection(&self, name: impl Into<String>) -> CreateCollection {
32        CreateCollection {
33            db: self,
34            name: name.into(),
35            options: None,
36            session: None,
37        }
38    }
39}
40
41#[cfg(feature = "sync")]
42impl crate::sync::Database {
43    /// Creates a new collection in the database with the given `name`.
44    ///
45    /// Note that MongoDB creates collections implicitly when data is inserted, so this method is
46    /// not needed if no special options are required.
47    ///
48    /// [`run`](CreateCollection::run) will return d[`Result<()>`].
49    #[deeplink]
50    #[options_doc(create_coll, sync)]
51    pub fn create_collection(&self, name: impl Into<String>) -> CreateCollection {
52        self.async_database.create_collection(name)
53    }
54}
55
56/// Creates a new collection.  Construct with [`Database::create_collection`].
57#[must_use]
58pub struct CreateCollection<'a> {
59    pub(crate) db: &'a Database,
60    pub(crate) name: String,
61    pub(crate) options: Option<CreateCollectionOptions>,
62    pub(crate) session: Option<&'a mut ClientSession>,
63}
64
65#[option_setters(crate::db::options::CreateCollectionOptions)]
66#[export_doc(create_coll)]
67impl<'a> CreateCollection<'a> {
68    /// Use the provided session when running the operation.
69    pub fn session(mut self, value: impl Into<&'a mut ClientSession>) -> Self {
70        self.session = Some(value.into());
71        self
72    }
73}
74
75// Action impl in src/db/action/create_collection.rs