1
+ import com .mongodb .ConnectionString ;
2
+ import com .mongodb .MongoClientSettings ;
3
+ import com .mongodb .ServerApi ;
4
+ import com .mongodb .ServerApiVersion ;
5
+
6
+ import com .mongodb .bulk .BulkWriteResult ;
7
+ import com .mongodb .client .model .InsertOneModel ;
8
+ import com .mongodb .client .model .UpdateOneModel ;
9
+ import com .mongodb .client .model .UpdateManyModel ;
10
+ import com .mongodb .client .model .ReplaceOneModel ;
11
+ import com .mongodb .client .model .DeleteOneModel ;
12
+ import com .mongodb .client .model .DeleteManyModel ;
13
+ import com .mongodb .client .model .BulkWriteOptions ;
14
+ import com .mongodb .reactivestreams .client .MongoCollection ;
15
+
16
+ import org .bson .Document ;
17
+
18
+ import com .mongodb .reactivestreams .client .MongoClient ;
19
+ import com .mongodb .reactivestreams .client .MongoClients ;
20
+ import com .mongodb .reactivestreams .client .MongoDatabase ;
21
+ import org .reactivestreams .Publisher ;
22
+ import reactor .core .publisher .Mono ;
23
+
24
+ import java .util .Arrays ;
25
+
26
+ import static com .mongodb .client .model .Filters .eq ;
27
+ import static com .mongodb .client .model .Updates .set ;
28
+
29
+ class BulkWrite {
30
+ public static void main (String [] args ) throws InterruptedException {
31
+ // Replace the placeholder with your Atlas connection string
32
+ String uri = "<connection string>" ;
33
+
34
+ // Construct a ServerApi instance using the ServerApi.builder() method
35
+ ServerApi serverApi = ServerApi .builder ()
36
+ .version (ServerApiVersion .V1 )
37
+ .build ();
38
+
39
+ MongoClientSettings settings = MongoClientSettings .builder ()
40
+ .applyConnectionString (new ConnectionString (uri ))
41
+ .serverApi (serverApi )
42
+ .build ();
43
+
44
+ // Create a new client and connect to the server
45
+ try (MongoClient mongoClient = MongoClients .create (settings )) {
46
+ MongoDatabase sample_restaurants = mongoClient .getDatabase ("sample_restaurants" );
47
+ MongoCollection <Document > restaurants = sample_restaurants .getCollection ("restaurants" );
48
+
49
+
50
+ // start-bulk-insert-one
51
+ InsertOneModel <Document > operation = new InsertOneModel <>(
52
+ new Document ("name" , "Mongo's Deli" )
53
+ .append ("cuisine" , "Sandwiches" ));
54
+ // end-bulk-insert-one
55
+
56
+ // start-bulk-update-one
57
+ UpdateOneModel <Document > operation = new UpdateOneModel <>(
58
+ eq ("name" , "Mongo's Deli" ),
59
+ set ("cuisine" , "Sandwiches and Salads" ));
60
+ // end-bulk-update-one
61
+
62
+ // start-bulk-update-many
63
+ UpdateManyModel <Document > operation = new UpdateManyModel <>(
64
+ eq ("name" , "Mongo's Deli" ),
65
+ set ("cuisine" , "Sandwiches and Salads" ));
66
+ // end-bulk-update-many
67
+
68
+ // start-bulk-replace-one
69
+ ReplaceOneModel <Document > operation = new ReplaceOneModel <>(
70
+ eq ("restaurant_id" , "1234" ),
71
+ new Document ("name" , "Mongo's Pizza" ));
72
+ // end-bulk-replace-one
73
+
74
+ // start-bulk-delete-one
75
+ DeleteOneModel <Document > operation = new DeleteOneModel <>(
76
+ eq ("restaurant_id" , "5678" ));
77
+ // end-bulk-delete-one
78
+
79
+ // start-bulk-delete-many
80
+ DeleteManyModel <Document > operation = new DeleteManyModel <>(
81
+ eq ("name" , "Mongo's Deli" ));
82
+ // end-bulk-delete-many
83
+
84
+ // start-bulk-write-mixed
85
+ Publisher <BulkWriteResult > bulkWritePublisher = restaurants .bulkWrite (
86
+ Arrays .asList (new InsertOneModel <>(
87
+ new Document ("name" , "Mongo's Deli" )
88
+ .append ("cuisine" , "Sandwiches" )
89
+ .append ("borough" , "Manhattan" )
90
+ .append ("restaurant_id" , "1234" )),
91
+ new InsertOneModel <>(new Document ("name" , "Mongo's Deli" )
92
+ .append ("cuisine" , "Sandwiches" )
93
+ .append ("borough" , "Brooklyn" )
94
+ .append ("restaurant_id" , "5678" )),
95
+ new UpdateManyModel <>(eq ("name" , "Mongo's Deli" ),
96
+ set ("cuisine" , "Sandwiches and Salads" )),
97
+ new DeleteOneModel <>(eq ("restaurant_id" , "1234" ))));
98
+
99
+ BulkWriteResult bulkResult = Mono .from (bulkWritePublisher ).block ();
100
+
101
+ System .out .printf (bulkResult .toString ());
102
+ // end-bulk-write-mixed
103
+
104
+ // start-bulk-write-unordered
105
+ Publisher <BulkWriteResult > bulkWritePublisher = restaurants .bulkWrite (
106
+ Arrays .asList (new InsertOneModel <>(
107
+ new Document ("name" , "Mongo's Deli" )
108
+ .append ("cuisine" , "Sandwiches" )
109
+ .append ("borough" , "Manhattan" )
110
+ .append ("restaurant_id" , "1234" )),
111
+ new InsertOneModel <>(new Document ("name" , "Mongo's Deli" )
112
+ .append ("cuisine" , "Sandwiches" )
113
+ .append ("borough" , "Brooklyn" )
114
+ .append ("restaurant_id" , "5678" )),
115
+ new UpdateManyModel <>(eq ("name" , "Mongo's Deli" ),
116
+ set ("cuisine" , "Sandwiches and Salads" )),
117
+ new DeleteOneModel <>(eq ("restaurant_id" , "1234" ))),
118
+ new BulkWriteOptions ().ordered (false ));
119
+
120
+ BulkWriteResult bulkResult = Mono .from (bulkWritePublisher ).block ();
121
+
122
+ System .out .printf (bulkResult .toString ());
123
+ // end-bulk-write-unordered
124
+
125
+ }
126
+ }
127
+
128
+ }
0 commit comments