@@ -8,4 +8,187 @@ Databases and Collections
88 :local:
99 :backlinks: none
1010 :depth: 2
11- :class: singlecol
11+ :class: singlecol
12+
13+ .. facet::
14+ :name: genre
15+ :values: reference
16+
17+ .. meta::
18+ :keywords: table, row, organize, storage, code example
19+
20+ Overview
21+ --------
22+
23+ In this guide, you can learn how to use MongoDB databases and
24+ collections with the {+driver-short+}.
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+ To access a database, pass the database name to the ``getDatabase()``
38+ method.
39+
40+ The following example accesses a database named ``test_database``:
41+
42+ .. literalinclude:: /includes/databases-collections.kt
43+ :language: kotlin
44+ :dedent:
45+ :start-after: start-access-database
46+ :end-before: end-access-database
47+
48+ .. _kotlin-sync-db-coll-access-collection:
49+
50+ Access a Collection
51+ -------------------
52+
53+ To access a collection, pass the database name to the ``getCollection()``
54+ method.
55+
56+ The following example accesses a collection named ``test_collection``:
57+
58+ .. literalinclude:: /includes/databases-collections.kt
59+ :language: kotlin
60+ :dedent:
61+ :start-after: start-access-collection
62+ :end-before: end-access-collection
63+
64+ .. tip::
65+
66+ If the provided collection name does not already exist in the database,
67+ MongoDB implicitly creates the collection when you first insert data
68+ into it.
69+
70+ .. _kotlin-sync-db-coll-create-collection:
71+
72+ Create a Collection
73+ -------------------
74+
75+ To explicitly create a collection in a MongoDB database, pass a collection
76+ name to the ``createCollection()`` method.
77+
78+ The following example creates a collection named ``example_collection``:
79+
80+ .. literalinclude:: /includes/databases-collections.kt
81+ :language: kotlin
82+ :dedent:
83+ :start-after: start-create-collection
84+ :end-before: end-create-collection
85+
86+ You can specify collection options, such as maximum size and document
87+ validation rules, by setting them in a ``CreateCollectionOptions`` instance.
88+ Then, pass the ``CreateCollectionOptions`` to the ``createCollection()`` method.
89+ For a full list of optional parameters, see the `CreateCollectionOptions <{+core-api+}/client/model/CreateCollectionOptions.html>`__
90+ API documentation.
91+
92+ Get a List of Collections
93+ -------------------------
94+
95+ You can query for a list of collections in a database by calling the
96+ ``listCollections()`` method. The method returns a cursor containing all
97+ collections in the database and their associated metadata.
98+
99+ The following example calls the ``listCollections()`` method and iterates over
100+ the returned iterator to print the collections from the :ref:`kotlin-sync-db-coll-access-collection`
101+ and :ref:`kotlin-sync-db-coll-create-collection` examples:
102+
103+ .. io-code-block::
104+ :copyable: true
105+
106+ .. input:: /includes/databases-collections.kt
107+ :language: kotlin
108+ :start-after: start-find-collections
109+ :end-before: end-find-collections
110+ :dedent:
111+
112+ .. output::
113+
114+ {
115+ "name": "example_collection",
116+ "type": "collection",
117+ "options": {},
118+ "info": {
119+ "readOnly": false,
120+ ...
121+ },
122+ "idIndex": { ... }
123+ }
124+ {
125+ "name": "test_collection",
126+ "type": "collection",
127+ "options": {},
128+ "info": {
129+ "readOnly": false,
130+ ...
131+ },
132+ "idIndex": { ... }
133+ }
134+
135+ Delete a Collection
136+ -------------------
137+
138+ To delete a collection from the database, call the ``drop()`` method
139+ on your collection.
140+
141+ The following example deletes the ``test_collection`` collection:
142+
143+ .. literalinclude:: /includes/databases-collections.kt
144+ :language: kotlin
145+ :dedent:
146+ :start-after: start-drop-collection
147+ :end-before: end-drop-collection
148+
149+ .. warning:: Dropping a Collection Deletes All Data in the Collection
150+
151+ Dropping a collection from your database permanently deletes all
152+ documents and all indexes within that collection.
153+
154+ Drop a collection only if you no longer need the data in it.
155+
156+ .. _kotlin-sync-config-read-write:
157+
158+ Configure Read and Write Operations
159+ -----------------------------------
160+
161+ You can control how read and write operations run on replica sets
162+ by specifying a read preference, read concern, or write concern.
163+
164+ By default, databases inherit read and write settings from the ``MongoClient``
165+ instance. Collections inherit these settings from the ``MongoClient`` or
166+ ``MongoDatabase`` instance on which the ``getCollection()`` method is called.
167+ You can change these settings by calling the following methods:
168+
169+ - `MongoDatabase.withReadConcern() <{+driver-api+}/-mongo-database/with-read-concern.html>`__
170+
171+ - `MongoDatabase.withReadPreference() <{+driver-api+}/-mongo-database/with-read-preference.html>`__
172+
173+ - `MongoDatabase.withWriteConcern() <{+driver-api+}/-mongo-database/with-write-concern.html>`__
174+
175+ - `MongoCollection.withReadConcern() <{+driver-api+}/-mongo-collection/with-read-concern.html>`__
176+
177+ - `MongoCollection.withReadPreference() <{+driver-api+}/-mongo-collection/with-read-preference.html>`__
178+
179+ - `MongoCollection.withWriteConcern() <{+driver-api+}/-mongo-collection/with-write-concern.html>`__
180+
181+ To learn more about setting a read preference, read concern, and write concern,
182+ see the :ref:`kotlin-sync-configure` guide.
183+
184+ API Documentation
185+ -----------------
186+
187+ To learn more about any of the methods or types discussed in this
188+ guide, see the following API documentation:
189+
190+ - `getDatabase() <{+driver-api+}/-mongo-cluster/get-database.html>`__
191+ - `getCollection() <{+driver-api+}/-mongo-database/get-collection.html>`__
192+ - `createCollection() <{+driver-api+}/-mongo-database/create-collection.html>`__
193+ - `listCollections() <{+driver-api+}/-mongo-database/list-collections.html>`__
194+ - `drop() <{+driver-api+}/-mongo-collection/drop.html>`__
0 commit comments