|
4 | 4 | Databases and Collections
|
5 | 5 | =========================
|
6 | 6 |
|
7 |
| -.. TODO |
| 7 | + |
| 8 | +.. contents:: On this page |
| 9 | + :local: |
| 10 | + :backlinks: none |
| 11 | + :depth: 2 |
| 12 | + :class: singlecol |
| 13 | + |
| 14 | +.. facet:: |
| 15 | + :name: genre |
| 16 | + :values: reference |
| 17 | + |
| 18 | +.. meta:: |
| 19 | + :keywords: table, row, organize, storage, code example |
| 20 | + :description: Learn how to manage MongoDB databases and collections using the Go driver, including accessing, creating, and deleting collections. |
| 21 | + |
| 22 | +Overview |
| 23 | +-------- |
| 24 | + |
| 25 | +In this guide, you can learn how to interact with MongoDB databases and |
| 26 | +collections by using the {+driver-short+}. |
| 27 | + |
| 28 | +MongoDB organizes data into a hierarchy of the following levels: |
| 29 | + |
| 30 | +- **Databases**: Top-level data structures in a MongoDB deployment that store |
| 31 | + collections. |
| 32 | +- **Collections**: Groups of MongoDB documents. They are analogous to tables in |
| 33 | + relational databases. |
| 34 | +- **Documents**: Units that store literal data such as strings, numbers, dates, |
| 35 | + and other embedded documents. For more information about document field types |
| 36 | + and structure, see the :manual:`Documents </core/document/>` entry in the |
| 37 | + {+mdb-server+} manual. |
| 38 | + |
| 39 | +Access a Database |
| 40 | +----------------- |
| 41 | + |
| 42 | +You can access a database by using the ``Database()`` method on a ``Client`` |
| 43 | +instance. |
| 44 | + |
| 45 | +The following example accesses a database named ``test_database``: |
| 46 | + |
| 47 | +.. literalinclude:: /includes/databases-collections.go |
| 48 | + :start-after: start-access-database |
| 49 | + :end-before: end-access-database |
| 50 | + :language: go |
| 51 | + :copyable: |
| 52 | + :dedent: |
| 53 | + |
| 54 | +Access a Collection |
| 55 | +------------------- |
| 56 | + |
| 57 | +You can access a collection by using the ``Collection()`` method on a |
| 58 | +``Database`` instance. |
| 59 | + |
| 60 | +The following example accesses a collection named ``test_collection``: |
| 61 | + |
| 62 | +.. literalinclude:: /includes/databases-collections.go |
| 63 | + :start-after: start-access-collection |
| 64 | + :end-before: end-access-collection |
| 65 | + :language: go |
| 66 | + :copyable: |
| 67 | + :dedent: |
| 68 | + |
| 69 | +.. tip:: |
| 70 | + |
| 71 | + If the provided collection name does not already exist in the database, |
| 72 | + MongoDB implicitly creates the collection when you first insert data into it. |
| 73 | + |
| 74 | +Create a Collection |
| 75 | +------------------- |
| 76 | + |
| 77 | +To explicity create a collection, you can use the ``CreateCollection()`` method |
| 78 | +on a ``Database`` instance. |
| 79 | + |
| 80 | +The following example creates a collection named ``example_collection``: |
| 81 | + |
| 82 | +.. literalinclude:: /includes/databases-collections.go |
| 83 | + :start-after: start-create-collection |
| 84 | + :end-before: end-create-collection |
| 85 | + :language: go |
| 86 | + :copyable: |
| 87 | + :dedent: |
| 88 | + |
| 89 | +You can specify collection options, such as maximum size and document validation |
| 90 | +rules, by passing an ``options.CreateCollectionOptions`` struct to the |
| 91 | +``CreateCollection()`` method of a ``Database`` instance. For a full list of |
| 92 | +optional parameters, see the :manual:`create command |
| 93 | +</reference/command/create>` entry in the {+mdb-server+} manual. |
| 94 | + |
| 95 | +Get a List of Collections |
| 96 | +------------------------- |
| 97 | + |
| 98 | +You can query for a list of collections in a database by using the |
| 99 | +``ListCollections()`` method on a ``Database`` instance. |
| 100 | + |
| 101 | +The following example gets a list of all the collections in a database, and then |
| 102 | +prints the collection names to the console: |
| 103 | + |
| 104 | +.. literalinclude:: /includes/databases-collections.go |
| 105 | + :start-after: start-list-collections |
| 106 | + :end-before: end-list-collections |
| 107 | + :language: go |
| 108 | + :copyable: |
| 109 | + :dedent: |
| 110 | + |
| 111 | +Delete a Collection |
| 112 | +------------------- |
| 113 | + |
| 114 | +You can delete a collection by using the ``Drop()`` method on a ``Database`` |
| 115 | +instance. |
| 116 | + |
| 117 | +The following example deletes a collection named |
| 118 | +``test_collection``: |
| 119 | + |
| 120 | +.. literalinclude:: /includes/databases-collections.go |
| 121 | + :start-after: start-delete-collection |
| 122 | + :end-before: end-delete-collection |
| 123 | + :language: go |
| 124 | + :copyable: |
| 125 | + :dedent: |
| 126 | + |
| 127 | +.. warning:: Dropping a Collection Deletes All Data in the Collection |
| 128 | + |
| 129 | + Dropping a collection from your database permanently deletes all documents |
| 130 | + and all indexes within that collection. |
| 131 | + |
| 132 | + Drop a collection only if you no longer need the data in that collection. |
| 133 | + |
| 134 | +API Documentation |
| 135 | +----------------- |
| 136 | + |
| 137 | +To learn more about any of the types or methods discussed in this guide, see the |
| 138 | +following API documentation: |
| 139 | + |
| 140 | +- `Client <{+api+}/mongo#Client>`__ |
| 141 | +- `Database <{+api+}/mongo#Database>`__ |
| 142 | +- `Collection <{+api+}/mongo#Collection>`__ |
| 143 | +- `CreateCollection() <{+api+}/mongo#Database.CreateCollection>`__ |
| 144 | +- `ListCollections() <{+api+}/mongo#Database.ListCollections>`__ |
| 145 | +- `Drop() <{+api+}/mongo#Database.Drop>`__ |
0 commit comments