Skip to content

Commit 65b18fe

Browse files
committed
add code
1 parent 77d075d commit 65b18fe

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

source/includes/write/bulk.kt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import com.mongodb.client.model.*
2+
import com.mongodb.client.model.Filters.*
3+
import com.mongodb.kotlin.client.MongoClient
4+
5+
// start-data-class
6+
data class Restaurant(val name: String, val borough: String, val cuisine: String)
7+
// end-data-class
8+
9+
fun main() {
10+
val uri = "<connection string>"
11+
12+
val mongoClient = MongoClient.create(uri)
13+
val database = mongoClient.getDatabase("sample_restaurants")
14+
val collection = database.getCollection<Restaurant>("restaurants")
15+
16+
// start-bulk-insert-one
17+
val blueMoon = InsertOneModel(Restaurant("Blue Moon Grill", "Brooklyn", "American"))
18+
// end-bulk-insert-one
19+
20+
// start-bulk-update-one
21+
val updateOneFilter = Filters.eq(Restaurant::name.name, "White Horse Tavern")
22+
val updateOneDoc = Updates.set(Restaurant::borough.name, "Queens")
23+
val tavernUpdate = UpdateOneModel<Restaurant>(updateOneFilter, updateOneDoc)
24+
// end-bulk-update-one
25+
26+
// start-bulk-update-many
27+
val updateManyFilter = Filters.eq(Restaurant::name.name, "Wendy's")
28+
val updateManyDoc = Updates.set(Restaurant::cuisine.name, "Fast food")
29+
val wendysUpdate = UpdateManyModel<Restaurant>(updateManyFilter, updateManyDoc)
30+
// end-bulk-update-many
31+
32+
// start-bulk-replace-one
33+
val replaceFilter = Filters.eq(Restaurant::name.name, "Cooper Town Diner")
34+
val replaceDoc = Restaurant("Smith Town Diner", "Brooklyn", "American")
35+
val replacement = ReplaceOneModel(replaceFilter, replaceDoc)
36+
// end-bulk-replace-one
37+
38+
// start-bulk-delete-one
39+
val deleteOne = DeleteOneModel<Restaurant>(Filters.eq(
40+
Restaurant::name.name,
41+
"Morris Park Bake Shop"
42+
))
43+
// end-bulk-delete-one
44+
45+
// start-bulk-delete-many
46+
val deleteMany = DeleteManyModel<Restaurant>(Filters.eq(
47+
Restaurant::cuisine.name,
48+
"Experimental"
49+
))
50+
// end-bulk-delete-many
51+
52+
// start-bulk-write-mixed
53+
val insertOneMdl = InsertOneModel(Restaurant("Red's Pizza", "Brooklyn", "Pizzeria"))
54+
val updateOneMdl = UpdateOneModel<Restaurant>(
55+
Filters.eq(Restaurant::name.name, "Moonlit Tavern"),
56+
Updates.set(Restaurant::borough.name, "Queens")
57+
)
58+
val deleteManyMdl = DeleteManyModel<Restaurant>(
59+
Filters.eq(Restaurant::name.name, "Crepe")
60+
)
61+
62+
val bulkResult = collection.bulkWrite(
63+
listOf(insertOneMdl, updateOneMdl, deleteManyMdl)
64+
)
65+
66+
println(bulkResult)
67+
// end-bulk-write-mixed
68+
69+
val bulkOperations = listOf(insertOneMdl, updateOneMdl, deleteManyMdl)
70+
71+
// start-bulk-write-unordered
72+
val opts = BulkWriteOptions().ordered(false)
73+
collection.bulkWrite(bulkOperations, opts)
74+
// end-bulk-write-unordered
75+
76+
}
77+

source/write/bulk-write.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,14 @@ The following example performs multiple write operations by using the
198198

199199
.. output::
200200

201-
201+
AcknowledgedBulkWriteResult{insertedCount=1, matchedCount=5, removedCount=3,
202+
modifiedCount=2, upserts=[], inserts=[BulkWriteInsert{index=0,
203+
id=BsonObjectId{value=...}}]}
202204

203205
If any of the write operations fail, {+driver-short+} raises a
204206
``BulkWriteError`` and does not perform any further operations.
205-
``BulkWriteError`` provides a ``details`` attribute that includes the operation
206-
that failed, and details about the exception.
207+
``BulkWriteError`` provides a ``details`` item that includes the
208+
operation that failed, and details about the exception.
207209

208210
.. note::
209211

0 commit comments

Comments
 (0)