@@ -15,7 +15,7 @@ Insert Operations
1515Overview
1616--------
1717
18- In this guide, you can learn how to insert documents with the MongoDB Java
18+ In this guide, you can learn how to insert documents with the MongoDB Kotlin
1919driver.
2020
2121You can use MongoDB to retrieve, update, and delete information. To
@@ -32,6 +32,12 @@ The following sections focus on ``insertOne()`` and
3232method, see our
3333:doc:`guide on Bulk Operations </fundamentals/crud/write-operations/bulk/>`.
3434
35+ In the following examples, a paint store has an inventory of different colors
36+ of paint. This data is modeled with the following Kotlin data class:
37+
38+ .. literalinclude:: /examples/generated/InsertTest.snippet.data-model.kt
39+ :language: kotlin
40+
3541A Note About ``_id``
3642--------------------
3743
@@ -70,24 +76,21 @@ Example
7076The following example creates and inserts a document using the
7177``insertOne()`` method:
7278
73- .. literalinclude:: /includes/fundamentals/code-snippets/Insert.java
74- :language: java
75- :dedent:
76- :start-after: begin insertOneExample
77- :end-before: end insertOneExample
78-
79- Your output should look something like this:
79+ .. io-code-block::
80+
81+ .. input:: /examples/generated/InsertTest.snippet.insert-one.kt
82+ :language: kotlin
8083
81- .. code-block :: none
82- :copyable: false
84+ .. output ::
85+ :language: console
8386
84- Inserted a document with the following id: 60930c39a982931c20ef6cd6
87+ Inserted a document with the following id: 60930c39a982931c20ef6cd6
8588
8689For more information about the methods and classes mentioned in this section,
8790see the following resources:
8891
89- - `insertOne() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#insertOne(TDocument )>`__ API Documentation
90- - `InsertOneResult <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/result/InsertOneResult.html >`__ API Documentation
92+ - `insertOne() <TODO:(DOCSP-29169 )>`__ API Documentation
93+ - `InsertOneResult <TODO:(DOCSP-29169) >`__ API Documentation
9194- Manual Explanation on :manual:`insertOne() </reference/method/db.collection.insertOne/>`
9295- Runnable :doc:`Insert a Document Example </usage-examples/insertOne>`
9396
@@ -102,10 +105,10 @@ For example, assume you want to insert the following documents:
102105
103106.. code-block:: json
104107
105- { "_id": 3, " color": "red", "qty": 5 }
106- { "_id": 4, " color": "purple", "qty": 10 }
107- { "_id": 3, " color": "yellow", "qty": 3 }
108- { "_id": 6, " color": "blue", "qty": 8 }
108+ { "color": "red", "qty": 5 }
109+ { "color": "purple", "qty": 10 }
110+ { "color": "yellow", "qty": 3 }
111+ { "color": "blue", "qty": 8 }
109112
110113If you attempt to insert these documents, a ``WriteError`` occurs at the
111114third document and the documents prior to the error get inserted into
@@ -114,30 +117,28 @@ your collection.
114117.. tip::
115118
116119 Use a try-catch block to get an acknowledgment for successfully
117- processed documents before the error occurs:
120+ processed documents before the error occurs. The output consists of
121+ documents MongoDB can process:
118122
119- .. literalinclude:: /includes/fundamentals/code-snippets/Insert.java
120- :language: java
121- :dedent:
122- :start-after: begin insertManyErrorExample
123- :end-before: end insertManyErrorExample
123+ .. io-code-block::
124+
125+ .. input:: /examples/generated/InsertTest.snippet.insert-many-error.kt
126+ :language: kotlin
124127
125- The output consists of documents MongoDB can process and should look
126- something like this:
128+ .. output::
129+ :language: console
127130
128- .. code-block::
129- :copyable: false
130-
131- A MongoBulkWriteException occurred, but there are successfully processed
132- documents with the following ids: [3, 4, 6]
131+ A MongoBulkWriteException occurred, but there are successfully processed
132+ documents with the following ids: [60930c3aa982931c20ef6cd7, 644ad1378ea29443837a14e9, 60930c3aa982931c20ef6cd8]
133+
133134
134135 If you look inside your collection, you should see the following documents:
135136
136137 .. code-block:: json
137138 :copyable: false
138139
139- { "_id": 3, " color": "red", "qty": 5 }
140- { "_id": 4, " color": "purple", "qty": 10 }
140+ { "color": "red", "qty": 5 }
141+ { "color": "purple", "qty": 10 }
141142
142143On successful insertion, the method returns an ``InsertManyResult``
143144instance representing the ``_id`` of each new document.
@@ -148,24 +149,21 @@ Example
148149The following example creates and adds two documents to a ``List``, and
149150inserts the ``List`` using the ``insertMany()`` method:
150151
151- .. literalinclude:: /includes/fundamentals/code-snippets/Insert.java
152- :language: java
153- :dedent:
154- :start-after: begin insertManyExample
155- :end-before: end insertManyExample
156-
157- Your output should look something like this:
152+ .. io-code-block::
153+
154+ .. input:: /examples/generated/InsertTest.snippet.insert-many.kt
155+ :language: kotlin
158156
159- .. code-block::
160- :copyable: false
157+ .. output::
158+ :language: console
161159
162- Inserted documents with the following ids: [60930c3aa982931c20ef6cd7, 60930c3aa982931c20ef6cd8]
160+ Inserted documents with the following ids: [60930c3aa982931c20ef6cd7, 60930c3aa982931c20ef6cd8]
163161
164162For more information about the methods and classes mentioned in this section,
165163see the following resources:
166164
167- - `insertMany() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#insertMany(java.util.List )>`__ API Documentation
168- - `InsertManyResult <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/result/InsertManyResult.html >`__ API Documentation
165+ - `insertMany() <TODO:(DOCSP-29169 )>`__ API Documentation
166+ - `InsertManyResult <TODO:(DOCSP-29169) >`__ API Documentation
169167- Manual Explanation on :manual:`insertMany() </reference/method/db.collection.insertMany/>`
170168- Runnable :doc:`Insert Multiple Documents Example </usage-examples/insertMany>`
171169
0 commit comments