Skip to content

Commit 65fff1e

Browse files
authored
DOCSP-41126: write operations (#5)
* DOCSP-41126: write operations * staging * wip * MM PR fixes 1
1 parent 8b2055d commit 65fff1e

File tree

4 files changed

+282
-1
lines changed

4 files changed

+282
-1
lines changed

source/includes/usage-examples/sample-app.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import com.mongodb.ConnectionString
22
import com.mongodb.MongoClientSettings
3+
import com.mongodb.client.model.*
34
import com.mongodb.kotlin.client.*
4-
import com.mongodb.client.model.Filters.*
55
import org.bson.Document
66

77
fun main() {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import com.mongodb.ConnectionString
2+
import com.mongodb.MongoClientSettings
3+
import com.mongodb.client.model.*
4+
import com.mongodb.kotlin.client.*
5+
import org.bson.Document
6+
7+
fun main() {
8+
val uri = "<connection string URI>"
9+
10+
val settings = MongoClientSettings.builder()
11+
.applyConnectionString(ConnectionString(uri))
12+
.retryWrites(true)
13+
.build()
14+
15+
// Create a new client and connect to the server
16+
val mongoClient = MongoClient.create(settings)
17+
val database = mongoClient.getDatabase("<database name>")
18+
val collection = database.getCollection<Document>("<collection name>")
19+
20+
// start-insert-one
21+
val result = collection.insertOne(Document("<field name>", "<value>"))
22+
print(result.insertedId)
23+
// end-insert-one
24+
25+
// start-insert-multiple
26+
val docList = listOf(
27+
Document("<field name>", "<value>"),
28+
Document("<field name>", "<value>")
29+
)
30+
31+
val result = collection.insertMany(docList)
32+
print(result.insertedIds)
33+
// end-insert-multiple
34+
35+
// start-update-one
36+
val query = Filters.eq("<field to match>", "<value to match>")
37+
val update = Updates.set("<field name>", "<value>")
38+
39+
val result = collection.updateOne(query, update)
40+
print(result.modifiedCount)
41+
// end-update-one
42+
43+
// start-update-multiple
44+
val query = Filters.eq("<field to match>", "<value to match>")
45+
val update = Updates.set("<field name>", "<value>")
46+
47+
val result = collection.updateMany(query, update)
48+
print(result.modifiedCount)
49+
// end-update-multiple
50+
51+
// start-replace-one
52+
val query = Filters.eq("<field to match>", "<value to match>")
53+
val replacement = Document("<new document field name>", "<new document value>")
54+
55+
val result = collection.replaceOne(query, replacement)
56+
print(result.modifiedCount)
57+
// end-replace-one
58+
59+
// start-delete-one
60+
val query = Filters.eq("<field to match>", "<value to match>")
61+
62+
val result = collection.deleteOne(query)
63+
print(result.deletedCount)
64+
// end-delete-one
65+
66+
// start-delete-multiple
67+
val query = Filters.eq("<field to match>", "<value to match>")
68+
69+
val result = collection.deleteMany(query)
70+
print(result.deletedCount)
71+
// end-delete-multiple
72+
73+
// start-bulk-write
74+
val bulkOps = listOf(
75+
InsertOneModel(Document("<field name>", "<value>")),
76+
UpdateOneModel(
77+
Filters.eq("<field to match>", "<value to match>"),
78+
Updates.set("<field name>", "<value>")),
79+
DeleteOneModel(Filters.eq("<field to match>", "<value to match>"))
80+
)
81+
82+
val result = collection.bulkWrite(bulkOps)
83+
print(result)
84+
// end-bulk-write
85+
}

source/index.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
.. toctree::
1515

1616
/read
17+
/write-operations
1718
/faq
1819
/connection-troubleshooting
1920
/issues-and-help
@@ -57,6 +58,16 @@ Quick Reference
5758
See driver syntax examples for common MongoDB commands in the
5859
:ref:`Quick Reference <kotlin-sync-quick-reference>` section.
5960

61+
Write Data to MongoDB
62+
---------------------
63+
64+
Learn how you can write data to MongoDB in the :ref:`kotlin-sync-write` section.
65+
66+
Read Data from MongoDB
67+
----------------------
68+
69+
Learn how you can retrieve data from MongoDB in the :ref:`kotlin-sync-read` section.
70+
6071
FAQ
6172
---
6273

source/write-operations.txt

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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

Comments
 (0)