|
| 1 | +package org.example; |
| 2 | + |
| 3 | +import com.mongodb.*; |
| 4 | +import com.mongodb.client.model.*; |
| 5 | +import com.mongodb.client.model.bulk.*; |
| 6 | +import org.bson.Document; |
| 7 | + |
| 8 | +import org.reactivestreams.Publisher; |
| 9 | +import reactor.core.publisher.Mono; |
| 10 | + |
| 11 | +import com.mongodb.reactivestreams.client.MongoClient; |
| 12 | +import com.mongodb.reactivestreams.client.MongoClients; |
| 13 | + |
| 14 | +import java.util.*; |
| 15 | + |
| 16 | +public class QuickStart { |
| 17 | + public static void main(String[] args) { |
| 18 | + // Replace the placeholder with your Atlas connection string |
| 19 | + String uri = "<connection string>"; |
| 20 | + |
| 21 | + MongoClientSettings settings = MongoClientSettings.builder() |
| 22 | + .applyConnectionString(new ConnectionString(uri)) |
| 23 | + .build(); |
| 24 | + |
| 25 | + // Create a new client and connect to the server |
| 26 | + try (MongoClient mongoClient = MongoClients.create(settings)) { |
| 27 | + |
| 28 | + // start-insert-models |
| 29 | + ClientNamespacedInsertOneModel personToInsert = ClientNamespacedWriteModel |
| 30 | + .insertOne( |
| 31 | + new MongoNamespace("db", "people"), |
| 32 | + new Document("name", "Julia Smith") |
| 33 | + ); |
| 34 | + |
| 35 | + ClientNamespacedInsertOneModel thingToInsert = ClientNamespacedWriteModel |
| 36 | + .insertOne( |
| 37 | + new MongoNamespace("db", "things"), |
| 38 | + new Document("object", "washing machine") |
| 39 | + ); |
| 40 | + // end-insert-models |
| 41 | + |
| 42 | + // start-replace-models |
| 43 | + ClientNamespacedReplaceOneModel personReplacement = ClientNamespacedWriteModel |
| 44 | + .replaceOne( |
| 45 | + new MongoNamespace("db", "people"), |
| 46 | + Filters.eq("_id", 1), |
| 47 | + new Document("name", "Frederic Hilbert") |
| 48 | + ); |
| 49 | + |
| 50 | + ClientNamespacedReplaceOneModel thingReplacement = ClientNamespacedWriteModel |
| 51 | + .replaceOne( |
| 52 | + new MongoNamespace("db", "things"), |
| 53 | + Filters.eq("_id", 1), |
| 54 | + new Document("object", "potato") |
| 55 | + ); |
| 56 | + // end-replace-models |
| 57 | + |
| 58 | + // start-perform |
| 59 | + MongoNamespace peopleNamespace = new MongoNamespace("db", "people"); |
| 60 | + MongoNamespace thingsNamespace = new MongoNamespace("db", "things"); |
| 61 | + |
| 62 | + List<ClientNamespacedWriteModel> bulkOperations = Arrays.asList( |
| 63 | + ClientNamespacedWriteModel |
| 64 | + .insertOne( |
| 65 | + peopleNamespace, |
| 66 | + new Document("name", "Corey Kopper") |
| 67 | + ), |
| 68 | + ClientNamespacedWriteModel |
| 69 | + .replaceOne( |
| 70 | + thingsNamespace, |
| 71 | + Filters.eq("_id", 1), |
| 72 | + new Document("object", "potato") |
| 73 | + ) |
| 74 | + ); |
| 75 | + |
| 76 | + Publisher<ClientBulkWriteResult> bulkWritePublisher = mongoClient |
| 77 | + .bulkWrite(bulkOperations); |
| 78 | + |
| 79 | + ClientBulkWriteResult clientBulkResult = Mono |
| 80 | + .from(bulkWritePublisher) |
| 81 | + .block(); |
| 82 | + |
| 83 | + System.out.println(clientBulkResult.toString()); |
| 84 | + // end-perform |
| 85 | + |
| 86 | + // start-options |
| 87 | + MongoNamespace namespace = new MongoNamespace("db", "people"); |
| 88 | + |
| 89 | + ClientBulkWriteOptions options = ClientBulkWriteOptions |
| 90 | + .clientBulkWriteOptions() |
| 91 | + .ordered(false); |
| 92 | + |
| 93 | + List<ClientNamespacedWriteModel> bulkOperations = Arrays.asList( |
| 94 | + ClientNamespacedWriteModel.insertOne( |
| 95 | + namespace, |
| 96 | + new Document("_id", 1).append("name", "Rudra Suraj") |
| 97 | + ), |
| 98 | + // Causes a duplicate key error |
| 99 | + ClientNamespacedWriteModel.insertOne( |
| 100 | + namespace, |
| 101 | + new Document("_id", 1).append("name", "Mario Bianchi") |
| 102 | + ), |
| 103 | + ClientNamespacedWriteModel.insertOne( |
| 104 | + namespace, |
| 105 | + new Document("name", "Wendy Zhang") |
| 106 | + ) |
| 107 | + ); |
| 108 | + |
| 109 | + Publisher<ClientBulkWriteResult> bulkWritePublisher = mongoClient |
| 110 | + .bulkWrite(bulkOperations, options); |
| 111 | + // end-options |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments