|
| 1 | +.. _kotlin-sync-write: |
| 2 | + |
| 3 | +===================== |
| 4 | +Write Data to MongoDB |
| 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 | + :description: Learn how to use the Kotlin Sync driver to write data to MongoDB. |
| 19 | + :keywords: usage examples, save, crud, create, code example |
| 20 | + |
| 21 | +.. .. toctree:: |
| 22 | +.. :titlesonly: |
| 23 | +.. :maxdepth: 1 |
| 24 | +.. |
| 25 | +.. /write/insert |
| 26 | +.. /write/update |
| 27 | +.. /write/replace |
| 28 | +.. /write/delete |
| 29 | +.. /write/bulk-write |
| 30 | +.. /write/gridfs |
| 31 | + |
| 32 | +Overview |
| 33 | +-------- |
| 34 | + |
| 35 | +On this page, you can see copyable code examples of common |
| 36 | +methods that you can use to write data to MongoDB by using the |
| 37 | +{+driver-short+}. |
| 38 | + |
| 39 | +.. tip:: |
| 40 | + |
| 41 | + To learn more about any of the methods shown on this page, see the link |
| 42 | + provided in each section. |
| 43 | + |
| 44 | +To use an example from this page, copy the code example into the |
| 45 | +:ref:`sample application <kotlin-sync-write-sample>` or your own application. |
| 46 | +Be sure to replace all placeholders in the code examples, such as ``<connection string URI>``, with |
| 47 | +the relevant values for your MongoDB deployment. |
| 48 | + |
| 49 | +.. _kotlin-sync-write-sample: |
| 50 | + |
| 51 | +.. include:: /includes/usage-examples/sample-app-intro.rst |
| 52 | + |
| 53 | +.. literalinclude:: /includes/usage-examples/sample-app.kt |
| 54 | + :language: kotlin |
| 55 | + :copyable: |
| 56 | + :linenos: |
| 57 | + :emphasize-lines: 20-22 |
| 58 | + |
| 59 | +Insert One |
| 60 | +---------- |
| 61 | + |
| 62 | +The following code shows how to insert a single document into a |
| 63 | +collection: |
| 64 | + |
| 65 | +.. literalinclude:: /includes/usage-examples/write-code-examples.kt |
| 66 | + :start-after: start-insert-one |
| 67 | + :end-before: end-insert-one |
| 68 | + :language: kotlin |
| 69 | + :copyable: |
| 70 | + :dedent: |
| 71 | + |
| 72 | +.. To learn more about the ``insert_one()`` method, see the :ref:`Insert Documents |
| 73 | +.. <kotlin-sync-write-insert>` guide. |
| 74 | + |
| 75 | +Insert Multiple |
| 76 | +--------------- |
| 77 | + |
| 78 | +The following code shows how to insert multiple documents into a |
| 79 | +collection: |
| 80 | + |
| 81 | +.. literalinclude:: /includes/usage-examples/write-code-examples.kt |
| 82 | + :start-after: start-insert-multiple |
| 83 | + :end-before: end-insert-multiple |
| 84 | + :language: kotlin |
| 85 | + :copyable: |
| 86 | + :dedent: |
| 87 | + |
| 88 | +.. To learn more about the ``insert_many()`` method, see the :ref:`Insert Documents |
| 89 | +.. <kotlin-sync-write-insert>` guide. |
| 90 | + |
| 91 | +Update One |
| 92 | +---------- |
| 93 | + |
| 94 | +The following code shows how to update a single document in a |
| 95 | +collection by creating or editing a field: |
| 96 | + |
| 97 | +.. literalinclude:: /includes/usage-examples/write-code-examples.kt |
| 98 | + :start-after: start-update-one |
| 99 | + :end-before: end-update-one |
| 100 | + :language: kotlin |
| 101 | + :copyable: |
| 102 | + :dedent: |
| 103 | + |
| 104 | +.. To learn more about the ``update_one()`` method, see the |
| 105 | +.. :ref:`Update Documents <kotlin-sync-write-update>` guide. |
| 106 | + |
| 107 | +Update Multiple |
| 108 | +--------------- |
| 109 | + |
| 110 | +The following code shows how to update multiple documents in a |
| 111 | +collection by creating or editing a field: |
| 112 | + |
| 113 | +.. literalinclude:: /includes/usage-examples/write-code-examples.kt |
| 114 | + :start-after: start-update-multiple |
| 115 | + :end-before: end-update-multiple |
| 116 | + :language: kotlin |
| 117 | + :copyable: |
| 118 | + :dedent: |
| 119 | + |
| 120 | +.. To learn more about the ``update_many()`` method, see the |
| 121 | +.. :ref:`Update Documents <kotlin-sync-write-update>` guide. |
| 122 | + |
| 123 | +Replace One |
| 124 | +----------- |
| 125 | + |
| 126 | +The following code shows how to replace a single document in a |
| 127 | +collection with a new document: |
| 128 | + |
| 129 | +.. literalinclude:: /includes/usage-examples/write-code-examples.kt |
| 130 | + :start-after: start-replace-one |
| 131 | + :end-before: end-replace-one |
| 132 | + :language: kotlin |
| 133 | + :copyable: |
| 134 | + :dedent: |
| 135 | + |
| 136 | +.. To learn more about the ``replace_one()`` method, see the |
| 137 | +.. :ref:`Replace Documents <kotlin-sync-write-replace>` guide. |
| 138 | + |
| 139 | +Delete One |
| 140 | +---------- |
| 141 | + |
| 142 | +The following code shows how to delete a single document in a |
| 143 | +collection: |
| 144 | + |
| 145 | +.. literalinclude:: /includes/usage-examples/write-code-examples.kt |
| 146 | + :start-after: start-delete-one |
| 147 | + :end-before: end-delete-one |
| 148 | + :language: kotlin |
| 149 | + :copyable: |
| 150 | + :dedent: |
| 151 | + |
| 152 | +.. To learn more about the ``delete_one()`` method, see the |
| 153 | +.. :ref:`Delete Documents <kotlin-sync-write-delete>` guide. |
| 154 | + |
| 155 | +Delete Multiple |
| 156 | +--------------- |
| 157 | + |
| 158 | +The following code shows how to delete multiple documents in a |
| 159 | +collection: |
| 160 | + |
| 161 | +.. literalinclude:: /includes/usage-examples/write-code-examples.kt |
| 162 | + :start-after: start-delete-multiple |
| 163 | + :end-before: end-delete-multiple |
| 164 | + :language: kotlin |
| 165 | + :copyable: |
| 166 | + :dedent: |
| 167 | + |
| 168 | +.. To learn more about the ``delete_many()`` method, see the |
| 169 | +.. :ref:`Delete Documents <kotlin-sync-write-delete>` guide. |
| 170 | + |
| 171 | +Bulk Write |
| 172 | +---------- |
| 173 | + |
| 174 | +The following code shows how to perform multiple write operations in a |
| 175 | +single bulk operation: |
| 176 | + |
| 177 | +.. literalinclude:: /includes/usage-examples/write-code-examples.kt |
| 178 | + :start-after: start-bulk-write |
| 179 | + :end-before: end-bulk-write |
| 180 | + :language: kotlin |
| 181 | + :copyable: |
| 182 | + :dedent: |
| 183 | + |
| 184 | +.. To learn more about the ``bulk_write()`` method, see the |
| 185 | +.. :ref:`Bulk Write <kotlin-sync-bulk-write>` guide. |
0 commit comments