|
| 1 | +.. _rust-db-coll: |
| 2 | + |
| 3 | +========================= |
| 4 | +Databases and Collections |
| 5 | +========================= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn how to use the {+driver-short+} to access |
| 17 | +and manage MongoDB databases and collections. |
| 18 | + |
| 19 | +MongoDB organizes data in a hierarchal structure. A MongoDB |
| 20 | +deployment contains one or more **databases**, and each database |
| 21 | +contains one or more **collections**. In each collection, MongoDB stores |
| 22 | +data as **documents** that contain field-and-value pairs. |
| 23 | + |
| 24 | +To learn more about the document data format, |
| 25 | +see :manual:`Documents </core/document/>` in the Server manual. |
| 26 | + |
| 27 | +Access a Database |
| 28 | +----------------- |
| 29 | + |
| 30 | +You can access a database by retrieving a `Database |
| 31 | +<{+api+}/struct.Database.html>`__ instance from your client. You can use |
| 32 | +a ``Database`` instance to perform database-level operations and access |
| 33 | +collections that the database contains. |
| 34 | + |
| 35 | +Call one of the following methods on a `Client |
| 36 | +<{+api+}/struct.Client.html>`__ instance to create a ``Database``: |
| 37 | + |
| 38 | +- `database() <{+api+}/struct.Client.html#method.database>`__: retrieve a database by its name |
| 39 | +- `database_with_options() <{+api+}/struct.Client.html#method.database_with_options>`__: set |
| 40 | + options (`DatabaseOptions <{+api+}/options/struct.DatabaseOptions.html>`__) while retrieving a |
| 41 | + database by its name |
| 42 | +- `default_database() <{+api+}/struct.Client.html#method.default_database>`__: access the |
| 43 | + default database specified for your ``Client`` instance |
| 44 | + |
| 45 | +.. TODO when client options page is completed. To learn how to specify |
| 46 | +.. the default database for your client, see :ref:`clientoptions`. |
| 47 | + |
| 48 | +If you pass the name of a nonexistent database to the ``database()`` or |
| 49 | +``database_with_options()`` methods, the driver still returns a |
| 50 | +``Database`` instance. When you insert any data into collection in this |
| 51 | +database, the server creates it. |
| 52 | + |
| 53 | +The following example uses the ``database()`` method to access a |
| 54 | +database called ``test_db``: |
| 55 | + |
| 56 | +.. literalinclude:: /includes/fundamentals/code-snippets/db-coll.rs |
| 57 | + :language: rust |
| 58 | + :dedent: |
| 59 | + :start-after: begin-database |
| 60 | + :end-before: end-database |
| 61 | + |
| 62 | +List Databases |
| 63 | +-------------- |
| 64 | + |
| 65 | +To see a list of your deployment's databases, call the |
| 66 | +`list_database_names() |
| 67 | +<{+api+}/struct.Client.html#method.list_database_names>`__ method on |
| 68 | +your ``Client`` instance. This |
| 69 | +method returns a ``Vec<String>`` type, a vector containing the database |
| 70 | +names as strings. |
| 71 | + |
| 72 | +To see detailed information about each database, call the `list_databases() <{+api+}/struct.Client.html#method.list_databases>`__ |
| 73 | +method on your ``Client`` instance. This method returns a |
| 74 | +``Vec<DatabaseSpecification>`` type. The ``DatabaseSpecification`` type |
| 75 | +contains fields describing each database, such as its size and whether |
| 76 | +it contains data. |
| 77 | + |
| 78 | +The following example shows how to print a list of databases by using |
| 79 | +the ``list_database_names()`` method: |
| 80 | + |
| 81 | +.. io-code-block:: |
| 82 | + |
| 83 | + .. input:: /includes/fundamentals/code-snippets/db-coll.rs |
| 84 | + :start-after: begin-list-db |
| 85 | + :end-before: end-list-db |
| 86 | + :language: rust |
| 87 | + :dedent: |
| 88 | + |
| 89 | + .. output:: |
| 90 | + :language: console |
| 91 | + :visible: false |
| 92 | + |
| 93 | + ["admin", "local", "test_db", ...] |
| 94 | + |
| 95 | +Drop a Database |
| 96 | +--------------- |
| 97 | + |
| 98 | +Dropping a database permanently deletes all of the data in that database's |
| 99 | +collections. To drop a database, call the `drop() |
| 100 | +<{+api+}/struct.Database.html#method.drop>`__ method |
| 101 | +on your ``Database`` instance. The following code shows |
| 102 | +how to drop a database referenced by the ``db`` variable: |
| 103 | + |
| 104 | +.. literalinclude:: /includes/fundamentals/code-snippets/db-coll.rs |
| 105 | + :language: rust |
| 106 | + :dedent: |
| 107 | + :start-after: begin-drop-db |
| 108 | + :end-before: end-drop-db |
| 109 | + |
| 110 | +.. warning:: Dropping a Database Deletes Data |
| 111 | + |
| 112 | + Dropping a database permanently deletes all |
| 113 | + documents in the database's collections and all indexes on those collections. |
| 114 | + After you drop a database, you cannot access or restore any of its data. |
| 115 | + |
| 116 | +Access a Collection |
| 117 | +------------------- |
| 118 | + |
| 119 | +You can access a collection by retrieving a `Collection |
| 120 | +<{+api+}/struct.Collection.html>`__ instance from your database. You |
| 121 | +can use a ``Collection`` instance to perform data operations, |
| 122 | +create aggregations, and manage indexes. Call one of the following |
| 123 | +methods on a ``Database`` instance to retrieve a ``Collection``: |
| 124 | + |
| 125 | +- `collection() <{+api+}/struct.Database.html#method.collection>`__: retrieve a collection by its name |
| 126 | +- `collection_with_options() <{+api+}/struct.Database.html#method.collection_with_options>`__: set |
| 127 | + options (`CollectionOptions <{+api+}/options/struct.CollectionOptions.html>`__) while accessing a |
| 128 | + collection by its name |
| 129 | + |
| 130 | +If you pass the name of a nonexistent collection to the ``collection()`` or |
| 131 | +``collection_with_options()`` methods, the driver still returns a |
| 132 | +``Collection`` instance. When you insert any data into this |
| 133 | +collection, the server creates it. To learn how to explicitly |
| 134 | +create a collection, see the :ref:`Create a Collection |
| 135 | +<rust-create-collection>` section of this guide. |
| 136 | + |
| 137 | +This example uses the ``collection_with_options()`` method to |
| 138 | +perform the following actions: |
| 139 | + |
| 140 | +- Access a collection called ``coll_xyz`` from a database referenced by |
| 141 | + the ``db`` variable |
| 142 | +- Set a write preference on the collection in the ``CollectionOptions`` type |
| 143 | + |
| 144 | +.. literalinclude:: /includes/fundamentals/code-snippets/db-coll.rs |
| 145 | + :language: rust |
| 146 | + :dedent: |
| 147 | + :start-after: begin-coll |
| 148 | + :end-before: end-coll |
| 149 | + |
| 150 | +To learn more about write concerns, see :manual:`Write Concern </reference/write-concern/>` in |
| 151 | +the Server manual. |
| 152 | + |
| 153 | +Collection Parameterization |
| 154 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 155 | + |
| 156 | +You must parameterize your ``Collection`` instance by specifying what |
| 157 | +data type, such as ``Document``, you want to serialize the collection's |
| 158 | +data into. When you call a method on a ``Collection`` instance that is |
| 159 | +parameterized with a specific type, the method accepts or returns |
| 160 | +instances of this type. |
| 161 | + |
| 162 | +The following example shows equivalent ways of parameterizing a |
| 163 | +collection with the ``Document`` type: |
| 164 | + |
| 165 | +.. code-block:: rust |
| 166 | + :copyable: false |
| 167 | + |
| 168 | + let my_coll: Collection<Document> = client.database("test_db").collection("coll_xyz"); |
| 169 | + let my_coll = client.database("test_db").collection::<Document>("coll_xyz"); |
| 170 | + |
| 171 | +.. tip:: |
| 172 | + |
| 173 | + We recommend that you parameterize your ``Collection`` instance with a |
| 174 | + custom type that models your data instead of the ``Document`` type. |
| 175 | + You can avoid repetitive serialization and validation by defining a |
| 176 | + type that models your specific data. |
| 177 | + |
| 178 | +.. TODO link to serialization guide |
| 179 | + |
| 180 | +.. _rust-create-collection: |
| 181 | + |
| 182 | +Create a Collection |
| 183 | +------------------- |
| 184 | + |
| 185 | +You can explicitly create a collection by calling the |
| 186 | +`create_collection() |
| 187 | +<{+api+}/struct.Database.html#method.create_collection>`__ method on a |
| 188 | +``Database`` instance. This method takes the collection name and an |
| 189 | +optional `CreateCollectionOptions |
| 190 | +<{+api+}/options/struct.CreateCollectionOptions.html>`__ type as |
| 191 | +parameters. You can use a ``Collection`` instance to perform data |
| 192 | +operations, create aggregations, and manage indexes. |
| 193 | + |
| 194 | +The following code shows how to create a collection called ``coll_abc`` |
| 195 | +within a database referenced by the ``db`` variable: |
| 196 | + |
| 197 | +.. literalinclude:: /includes/fundamentals/code-snippets/db-coll.rs |
| 198 | + :language: rust |
| 199 | + :dedent: |
| 200 | + :start-after: begin-create-coll |
| 201 | + :end-before: end-create-coll |
| 202 | + |
| 203 | +.. TODO While creating a collection, you can implement document validation to |
| 204 | +.. maintain a consistent document schema and control which write operations |
| 205 | +.. succeed. To learn how to enable this feature, see the guide on :ref:`rust-doc-validation`. |
| 206 | + |
| 207 | +List Collections |
| 208 | +---------------- |
| 209 | + |
| 210 | +To see the names of the collections in a database, call the |
| 211 | +`list_collection_names() <{+api+}/struct.Database.html#method.list_collection_names>`__ method on your ``Database`` instance. This |
| 212 | +method returns a ``Vec<String>`` type, a vector containing the |
| 213 | +collection names as strings. |
| 214 | + |
| 215 | +To see detailed information about each collection, call the `list_collections() <{+api+}/struct.Database.html#method.list_collections>`__ |
| 216 | +method on your ``Database`` instance. This method returns a |
| 217 | +``Vec<CollectionSpecification>`` type. The `CollectionSpecification |
| 218 | +<{+api+}/struct.CollectionSpecification.html>`__ type |
| 219 | +contains fields describing each collection, such as its type and settings. |
| 220 | + |
| 221 | +The following example shows how to print the names of the collections in |
| 222 | +a database referenced by the ``db`` variable by using the |
| 223 | +``list_collection_names()`` method: |
| 224 | + |
| 225 | +.. io-code-block:: |
| 226 | + |
| 227 | + .. input:: /includes/fundamentals/code-snippets/db-coll.rs |
| 228 | + :start-after: begin-list-coll |
| 229 | + :end-before: end-list-coll |
| 230 | + :language: rust |
| 231 | + :dedent: |
| 232 | + |
| 233 | + .. output:: |
| 234 | + :language: console |
| 235 | + :visible: false |
| 236 | + |
| 237 | + ["my_coll", "coll_xyz", ...] |
| 238 | + |
| 239 | +Drop a Collection |
| 240 | +----------------- |
| 241 | + |
| 242 | +Dropping a collection permanently deletes all of the data in that |
| 243 | +collection. To drop a collection, call the `drop() |
| 244 | +<{+api+}/struct.Collection.html#method.drop>`__ method |
| 245 | +on your ``Collection`` instance. The following code shows |
| 246 | +how to drop a collection referenced by the ``my_coll`` variable: |
| 247 | + |
| 248 | +.. literalinclude:: /includes/fundamentals/code-snippets/db-coll.rs |
| 249 | + :language: rust |
| 250 | + :dedent: |
| 251 | + :start-after: begin-drop-coll |
| 252 | + :end-before: end-drop-coll |
| 253 | + |
| 254 | +.. warning:: Dropping a Collection Deletes Data |
| 255 | + |
| 256 | + Dropping a collection from your database permanently deletes all |
| 257 | + documents within that collection and all indexes on that collection. |
| 258 | + After you drop a collection, you cannot access or restore any of its data. |
| 259 | + |
| 260 | +Additional Information |
| 261 | +---------------------- |
| 262 | + |
| 263 | +For more information about the concepts in this guide, see the following pages: |
| 264 | + |
| 265 | +- :ref:`Insert Documents <rust-insert-guide>` guide in the |
| 266 | + {+driver-short+} documentation |
| 267 | +- :manual:`Databases and Collections </core/databases-and-collections/>` |
| 268 | + in the Server manual |
| 269 | +- :manual:`Documents </core/document/>` in the Server manual |
0 commit comments