|
| 1 | +.. _cpp-databases-collections: |
| 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 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: table, row, organize, storage |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the {+driver-short+} to interact |
| 24 | +with MongoDB databases and collections. |
| 25 | + |
| 26 | +MongoDB organizes data into a hierarchy of the following levels: |
| 27 | + |
| 28 | +- **Databases**: Top-level data structures in a MongoDB deployment that store collections. |
| 29 | +- **Collections**: Groups of MongoDB documents. They are analogous to tables in relational databases. |
| 30 | +- **Documents**: Units that store literal data such as string, numbers, dates, and other embedded documents. |
| 31 | + For more information about document field types and structure, see the |
| 32 | + :manual:`Documents </core/document/>` guide in the {+mdb-server+} manual. |
| 33 | + |
| 34 | +Access a Database |
| 35 | +----------------- |
| 36 | + |
| 37 | +You can access a database by calling the ``database()`` function on |
| 38 | +a ``mongocxx::client`` object and passing the name of the database as |
| 39 | +an argument. |
| 40 | + |
| 41 | +The following example accesses a database named ``"test_database"``: |
| 42 | + |
| 43 | +.. literalinclude:: /includes/databases-collections/databases-collections.cpp |
| 44 | + :start-after: start-access-database-method |
| 45 | + :end-before: end-access-database-method |
| 46 | + :language: cpp |
| 47 | + :copyable: |
| 48 | + :dedent: |
| 49 | + |
| 50 | +Alternatively, you can use the ``[]`` operator on a ``mongocxx::client`` |
| 51 | +as a shorthand for the ``database()`` function, as shown in the following code: |
| 52 | + |
| 53 | +.. literalinclude:: /includes/databases-collections/databases-collections.cpp |
| 54 | + :start-after: start-access-database-operator |
| 55 | + :end-before: end-access-database-operator |
| 56 | + :language: cpp |
| 57 | + :copyable: |
| 58 | + :dedent: |
| 59 | + |
| 60 | +Access a Collection |
| 61 | +------------------- |
| 62 | + |
| 63 | +You can access a collection by calling the ``collection()`` function on |
| 64 | +a ``mongocxx::database`` object and passing the name of the collection as |
| 65 | +an argument. |
| 66 | + |
| 67 | +The following example accesses a collection named ``"test_collection"``: |
| 68 | + |
| 69 | +.. literalinclude:: /includes/databases-collections/databases-collections.cpp |
| 70 | + :start-after: start-access-collection-method |
| 71 | + :end-before: end-access-collection-method |
| 72 | + :language: cpp |
| 73 | + :copyable: |
| 74 | + :dedent: |
| 75 | + |
| 76 | +Alternatively, you can use the ``[]`` operator on a ``mongocxx::database`` |
| 77 | +as a shorthand for the ``collection()`` function, as shown in the following code: |
| 78 | + |
| 79 | +.. literalinclude:: /includes/databases-collections/databases-collections.cpp |
| 80 | + :start-after: start-access-collection-operator |
| 81 | + :end-before: end-access-collection-operator |
| 82 | + :language: cpp |
| 83 | + :copyable: |
| 84 | + :dedent: |
| 85 | + |
| 86 | +.. tip:: |
| 87 | + |
| 88 | + If the provided collection name does not already exist in the database, |
| 89 | + MongoDB implicitly creates the collection when you first insert data |
| 90 | + into it. |
| 91 | + |
| 92 | +Create a Collection |
| 93 | +------------------- |
| 94 | + |
| 95 | +You can use the ``create_collection()`` function to explicitly create a collection in a |
| 96 | +MongoDB database. |
| 97 | + |
| 98 | +The following example creates a collection called ``"example_collection"``: |
| 99 | + |
| 100 | +.. literalinclude:: /includes/databases-collections/databases-collections.cpp |
| 101 | + :start-after: start-create-collection |
| 102 | + :end-before: end-create-collection |
| 103 | + :language: cpp |
| 104 | + :copyable: |
| 105 | + :dedent: |
| 106 | + |
| 107 | +You can specify collection options, such as maximum size and document |
| 108 | +validation rules, by passing them inside a BSON document as the second parameter |
| 109 | +to the ``create_collection()`` function. For a full list of |
| 110 | +optional parameters, see the :manual:`create command </reference/command/create>` |
| 111 | +documentation in the {+mdb-server+} manual. |
| 112 | + |
| 113 | +Get a List of Collections |
| 114 | +------------------------- |
| 115 | + |
| 116 | +You can retrieve a list of collections in a database by calling the |
| 117 | +``list_collections()`` function. The function returns a cursor containing all |
| 118 | +collections in the database and their associated metadata. |
| 119 | + |
| 120 | +The following example calls the ``list_collections()`` function and iterates over |
| 121 | +the cursor to print the results: |
| 122 | + |
| 123 | +.. io-code-block:: |
| 124 | + :copyable: |
| 125 | + |
| 126 | + .. input:: /includes/databases-collections/databases-collections.cpp |
| 127 | + :language: cpp |
| 128 | + :start-after: start-find-collections |
| 129 | + :end-before: end-find-collections |
| 130 | + :dedent: |
| 131 | + |
| 132 | + .. output:: |
| 133 | + :language: cpponsole |
| 134 | + :visible: false |
| 135 | + |
| 136 | + Collection: { "name" : "test_collection", "type" : "collection", ...} |
| 137 | + Collection: { "name" : "example_collection", "type" : "collection", ... } |
| 138 | + |
| 139 | +To query for only the names of the collections in the database, call the |
| 140 | +``list_collection_names()`` function as shown in the following example: |
| 141 | + |
| 142 | +.. io-code-block:: |
| 143 | + :copyable: |
| 144 | + |
| 145 | + .. input:: /includes/databases-collections/databases-collections.cpp |
| 146 | + :language: cpp |
| 147 | + :start-after: start-find-collection-names |
| 148 | + :end-before: end-find-collection-names |
| 149 | + :dedent: |
| 150 | + |
| 151 | + .. output:: |
| 152 | + :language: console |
| 153 | + :visible: false |
| 154 | + |
| 155 | + test_collection |
| 156 | + example_collection |
| 157 | + |
| 158 | +For more information about iterating over a cursor, see the :ref:`cpp-cursors` |
| 159 | +guide. |
| 160 | + |
| 161 | +Delete a Collection |
| 162 | +------------------- |
| 163 | + |
| 164 | +You can delete a collection from the database by using the ``drop()`` |
| 165 | +function. |
| 166 | + |
| 167 | +The following example deletes the ``"test_collection"`` collection: |
| 168 | + |
| 169 | +.. literalinclude:: /includes/databases-collections/databases-collections.cpp |
| 170 | + :start-after: start-delete-collection |
| 171 | + :end-before: end-delete-collection |
| 172 | + :language: cpp |
| 173 | + :copyable: |
| 174 | + :dedent: |
| 175 | + |
| 176 | +.. warning:: Dropping a Collection Deletes All Data in the Collection |
| 177 | + |
| 178 | + Dropping a collection from your database permanently deletes all |
| 179 | + documents and all indexes within that collection. |
| 180 | + |
| 181 | + Drop a collection only if the data in it is no longer needed. |
| 182 | + |
| 183 | +.. _cpp-config-read-write: |
| 184 | + |
| 185 | +Configure Read and Write Operations |
| 186 | +----------------------------------- |
| 187 | + |
| 188 | +You can control how the driver routes read operations by setting a **read preference**. |
| 189 | +You can also control options for how the driver waits for acknowledgment of |
| 190 | +read and write operations on a replica set by setting a **read concern** and a |
| 191 | +**write concern**. |
| 192 | + |
| 193 | +By default, databases inherit these settings from the ``mongocxx::client`` object, |
| 194 | +and collections inherit them from the database. However, you can change these |
| 195 | +settings by using one of the following functions on your database or collection: |
| 196 | + |
| 197 | +- ``read_preference()`` |
| 198 | +- ``read_concern()`` |
| 199 | +- ``write_concern()`` |
| 200 | + |
| 201 | +To learn more about read and write settings, see the following guides in the |
| 202 | +{+mdb-server+} manual: |
| 203 | + |
| 204 | +- :manual:`Read Preference </core/read-preference/>` |
| 205 | +- :manual:`Read Concern </reference/read-concern/>` |
| 206 | +- :manual:`Write Concern </reference/write-concern/>` |
| 207 | + |
| 208 | +Configure Database Settings |
| 209 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 210 | + |
| 211 | +This example shows how to configure read settings for your database |
| 212 | +by using the following functions: |
| 213 | + |
| 214 | +- ``read_preference()``: Sets the read preference to ``k_secondary`` |
| 215 | +- ``read_concern()``: Sets the read concern to ``k_majority`` |
| 216 | + |
| 217 | +.. literalinclude:: /includes/databases-collections/databases-collections.cpp |
| 218 | + :start-after: start-database-read-settings |
| 219 | + :end-before: end-database-read-settings |
| 220 | + :language: cpp |
| 221 | + :copyable: |
| 222 | + :dedent: |
| 223 | + |
| 224 | +.. tip:: |
| 225 | + |
| 226 | + To see a description of each read preference and read concern option, see the |
| 227 | + following API documentation: |
| 228 | + |
| 229 | + - `Read preference modes <{+api+}/classmongocxx_1_1v__noabi_1_1read__preference.html#a7e9a58e6c82169d2eb569f7993325154>`__ |
| 230 | + - `Read concern levels <{+api+}/classmongocxx_1_1v__noabi_1_1read__concern.html#a795c8037f826a1e64e052997fde61407>`__ |
| 231 | + |
| 232 | +Configure Collection Settings |
| 233 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 234 | + |
| 235 | +This example shows how to specify your collection's read and write concern |
| 236 | +by using the following functions: |
| 237 | + |
| 238 | +- ``read_concern()``: Sets the read concern to ``k_local`` |
| 239 | +- ``write_concern()``: Sets the write concern to ``k_acknowledged`` |
| 240 | + |
| 241 | +.. literalinclude:: /includes/databases-collections/databases-collections.cpp |
| 242 | + :start-after: start-collection-read-write-settings |
| 243 | + :end-before: end-collection-read-write-settings |
| 244 | + :language: cpp |
| 245 | + :copyable: |
| 246 | + :dedent: |
| 247 | + |
| 248 | +.. tip:: |
| 249 | + |
| 250 | + To see a description of each read and write concern level, see the |
| 251 | + following API documentation: |
| 252 | + |
| 253 | + - `Read concern levels <{+api+}/classmongocxx_1_1v__noabi_1_1read__concern.html#a795c8037f826a1e64e052997fde61407>`__ |
| 254 | + - `Write concern levels <{+api+}/classmongocxx_1_1v__noabi_1_1write__concern.html#a756cc9e4f51467924887b2ceda9c8856>`__ |
| 255 | + |
| 256 | +Tag Sets |
| 257 | +~~~~~~~~ |
| 258 | + |
| 259 | +In the {+mdb-server+}, you can apply key-value :manual:`tags |
| 260 | +</core/read-preference-tags/>` to replica-set |
| 261 | +members according to any criteria you choose. You can then use |
| 262 | +those tags to target one or more members for a read operation. |
| 263 | + |
| 264 | +By default, the {+driver-short+} ignores tags |
| 265 | +when choosing a member to read from. To instruct the {+driver-short+} |
| 266 | +to prefer certain tags, create a ``mongocxx::read_preference`` object |
| 267 | +and call its ``tags()`` member function. Pass your preferred tags as |
| 268 | +an array argument to ``tags()``. |
| 269 | + |
| 270 | +In the following code example, the tag set passed to the ``tags()`` |
| 271 | +function instructs the {+driver-short+} to prefer reads from the |
| 272 | +New York data center (``"dc": "ny"``) and to fall back to the San Francisco data |
| 273 | +center (``"dc": "sf"``): |
| 274 | + |
| 275 | +.. literalinclude:: /includes/databases-collections/databases-collections.cpp |
| 276 | + :start-after: start-tags |
| 277 | + :end-before: end-tags |
| 278 | + :language: cpp |
| 279 | + :copyable: |
| 280 | + :dedent: |
| 281 | + |
| 282 | +Local Threshold |
| 283 | +~~~~~~~~~~~~~~~ |
| 284 | + |
| 285 | +If multiple replica-set members match the read preference and tag sets you specify, |
| 286 | +the {+driver-short+} reads from the nearest replica-set members, chosen according to |
| 287 | +their ping time. |
| 288 | + |
| 289 | +By default, the driver uses only those members whose ping times are within 15 milliseconds |
| 290 | +of the nearest member for queries. To distribute reads between members with |
| 291 | +higher latencies, include the ``localThresholdMS`` parameter in your connection string URI. |
| 292 | + |
| 293 | +The following example connects to a MongoDB deployment running on ``localhost:27017`` |
| 294 | +and specifies a local threshold of 35 milliseconds: |
| 295 | + |
| 296 | +.. literalinclude:: /includes/databases-collections/databases-collections.cpp |
| 297 | + :start-after: start-local-threshold |
| 298 | + :end-before: end-local-threshold |
| 299 | + :language: cpp |
| 300 | + :copyable: |
| 301 | + :dedent: |
| 302 | + |
| 303 | +In the preceding example, the {+driver-short+} distributes reads between matching members |
| 304 | +within 35 milliseconds of the closest member's ping time. |
| 305 | + |
| 306 | +API Documentation |
| 307 | +----------------- |
| 308 | + |
| 309 | +To learn more about any of the functions discussed in this |
| 310 | +guide, see the following API documentation: |
| 311 | + |
| 312 | +- `database() <{+api+}/classmongocxx_1_1v__noabi_1_1client.html#ae28b50918e732e84ff78beb5748e3364>`__ |
| 313 | +- `collection() <{+api+}/classmongocxx_1_1v__noabi_1_1database.html#aba36d8296f118306e92168b1b72d04c4>`__ |
| 314 | +- `create_collection() <{+api+}/classmongocxx_1_1v__noabi_1_1database.html#a77bec925146cb2dfd395b0a46d5be3a6>`__ |
| 315 | +- `list_collections() <{+api+}/classmongocxx_1_1v__noabi_1_1database.html#aacef87f0bc585c536ce0dfae67cfefe8>`__ |
| 316 | +- `list_collection_names() <{+api+}/classmongocxx_1_1v__noabi_1_1database.html#a96f96c0fc00c1fc30c8151577cff935a>`__ |
| 317 | +- `drop() <{+api+}/classmongocxx_1_1v__noabi_1_1collection.html#a693cb2671c724f8a01e47339928283cb>`__ |
| 318 | +- `read_preference() <{+api+}/classmongocxx_1_1v__noabi_1_1database.html#ab9fe9fd6ffe5c3811e9fbb7a7d7fe5bc>`__ |
| 319 | +- `read_concern() <{+api+}/classmongocxx_1_1v__noabi_1_1database.html#a0bac544e0439575b673a7f25c8abc356>`__ |
| 320 | +- `write_concern() <{+api+}/classmongocxx_1_1v__noabi_1_1database.html#a4ae21da062a6bf0870cc98337f09ed7a>`__ |
0 commit comments