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 ("name" , "Original Pizza" ),
71+ new Document ("name" , "Mongo's Pizza" )
72+ .append ("borough" , "Manhattan" ));
73+ // end-bulk-replace-one
74+
75+ // start-bulk-delete-one
76+ DeleteOneModel <Document > operation = new DeleteOneModel <>(
77+ eq ("restaurant_id" , "5678" ));
78+ // end-bulk-delete-one
79+
80+ // start-bulk-delete-many
81+ DeleteManyModel <Document > operation = new DeleteManyModel <>(
82+ eq ("name" , "Mongo's Deli" ));
83+ // end-bulk-delete-many
84+
85+ // start-bulk-write-mixed
86+ Publisher <BulkWriteResult > bulkWritePublisher = restaurants .bulkWrite (
87+ Arrays .asList (new InsertOneModel <>(
88+ new Document ("name" , "Mongo's Deli" )
89+ .append ("cuisine" , "Sandwiches" )
90+ .append ("borough" , "Manhattan" )
91+ .append ("restaurant_id" , "1234" )),
92+ new InsertOneModel <>(new Document ("name" , "Mongo's Deli" )
93+ .append ("cuisine" , "Sandwiches" )
94+ .append ("borough" , "Brooklyn" )
95+ .append ("restaurant_id" , "5678" )),
96+ new UpdateManyModel <>(eq ("name" , "Mongo's Deli" ),
97+ set ("cuisine" , "Sandwiches and Salads" )),
98+ new DeleteOneModel <>(eq ("restaurant_id" , "1234" ))));
99+
100+ BulkWriteResult bulkResult = Mono .from (bulkWritePublisher ).block ();
101+
102+ System .out .println (bulkResult .toString ());
103+ // end-bulk-write-mixed
104+
105+ // start-bulk-write-unordered
106+ Publisher <BulkWriteResult > bulkWritePublisher = restaurants .bulkWrite (
107+ Arrays .asList (new InsertOneModel <>(
108+ new Document ("name" , "Mongo's Deli" )
109+ .append ("cuisine" , "Sandwiches" )
110+ .append ("borough" , "Manhattan" )
111+ .append ("restaurant_id" , "1234" )),
112+ new InsertOneModel <>(new Document ("name" , "Mongo's Deli" )
113+ .append ("cuisine" , "Sandwiches" )
114+ .append ("borough" , "Brooklyn" )
115+ .append ("restaurant_id" , "5678" )),
116+ new UpdateManyModel <>(eq ("name" , "Mongo's Deli" ),
117+ set ("cuisine" , "Sandwiches and Salads" )),
118+ new DeleteOneModel <>(eq ("restaurant_id" , "1234" ))),
119+ new BulkWriteOptions ().ordered (false ));
120+
121+ BulkWriteResult bulkResult = Mono .from (bulkWritePublisher ).block ();
122+
123+ System .out .println (bulkResult .toString ());
124+ // end-bulk-write-unordered
125+
126+ }
127+ }
128+
129+ }
0 commit comments