-
Notifications
You must be signed in to change notification settings - Fork 17
DOCSP-39699: Bulk writes #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import com.mongodb.ConnectionString; | ||
| import com.mongodb.MongoClientSettings; | ||
| import com.mongodb.ServerApi; | ||
| import com.mongodb.ServerApiVersion; | ||
|
|
||
| import com.mongodb.bulk.BulkWriteResult; | ||
| import com.mongodb.client.model.InsertOneModel; | ||
| import com.mongodb.client.model.UpdateOneModel; | ||
| import com.mongodb.client.model.UpdateManyModel; | ||
| import com.mongodb.client.model.ReplaceOneModel; | ||
| import com.mongodb.client.model.DeleteOneModel; | ||
| import com.mongodb.client.model.DeleteManyModel; | ||
| import com.mongodb.client.model.BulkWriteOptions; | ||
| import com.mongodb.reactivestreams.client.MongoCollection; | ||
|
|
||
| import org.bson.Document; | ||
|
|
||
| import com.mongodb.reactivestreams.client.MongoClient; | ||
| import com.mongodb.reactivestreams.client.MongoClients; | ||
| import com.mongodb.reactivestreams.client.MongoDatabase; | ||
| import org.reactivestreams.Publisher; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| import static com.mongodb.client.model.Filters.eq; | ||
| import static com.mongodb.client.model.Updates.set; | ||
|
|
||
| class BulkWrite { | ||
| public static void main(String[] args) throws InterruptedException { | ||
| // Replace the placeholder with your Atlas connection string | ||
| String uri = "<connection string>"; | ||
|
|
||
| // Construct a ServerApi instance using the ServerApi.builder() method | ||
| ServerApi serverApi = ServerApi.builder() | ||
| .version(ServerApiVersion.V1) | ||
| .build(); | ||
|
|
||
| MongoClientSettings settings = MongoClientSettings.builder() | ||
| .applyConnectionString(new ConnectionString(uri)) | ||
| .serverApi(serverApi) | ||
| .build(); | ||
|
|
||
| // Create a new client and connect to the server | ||
| try (MongoClient mongoClient = MongoClients.create(settings)) { | ||
| MongoDatabase sample_restaurants = mongoClient.getDatabase("sample_restaurants"); | ||
| MongoCollection<Document> restaurants = sample_restaurants.getCollection("restaurants"); | ||
|
|
||
|
|
||
| // start-bulk-insert-one | ||
| InsertOneModel<Document> operation = new InsertOneModel<>( | ||
| new Document("name", "Mongo's Deli") | ||
| .append("cuisine", "Sandwiches")); | ||
| // end-bulk-insert-one | ||
|
|
||
| // start-bulk-update-one | ||
| UpdateOneModel<Document> operation = new UpdateOneModel<>( | ||
| eq("name", "Mongo's Deli"), | ||
| set("cuisine", "Sandwiches and Salads")); | ||
| // end-bulk-update-one | ||
|
|
||
| // start-bulk-update-many | ||
| UpdateManyModel<Document> operation = new UpdateManyModel<>( | ||
| eq("name", "Mongo's Deli"), | ||
| set("cuisine", "Sandwiches and Salads")); | ||
| // end-bulk-update-many | ||
|
|
||
| // start-bulk-replace-one | ||
| ReplaceOneModel<Document> operation = new ReplaceOneModel<>( | ||
| eq("restaurant_id", "1234"), | ||
| new Document("name", "Mongo's Pizza")); | ||
| // end-bulk-replace-one | ||
|
|
||
| // start-bulk-delete-one | ||
| DeleteOneModel<Document> operation = new DeleteOneModel<>( | ||
| eq("restaurant_id", "5678")); | ||
| // end-bulk-delete-one | ||
|
|
||
| // start-bulk-delete-many | ||
| DeleteManyModel<Document> operation = new DeleteManyModel<>( | ||
| eq("name", "Mongo's Deli")); | ||
| // end-bulk-delete-many | ||
|
|
||
| // start-bulk-write-mixed | ||
| Publisher<BulkWriteResult> bulkWritePublisher = restaurants.bulkWrite( | ||
| Arrays.asList(new InsertOneModel<>( | ||
| new Document("name", "Mongo's Deli") | ||
| .append("cuisine", "Sandwiches") | ||
| .append("borough", "Manhattan") | ||
| .append("restaurant_id", "1234")), | ||
| new InsertOneModel<>(new Document("name", "Mongo's Deli") | ||
| .append("cuisine", "Sandwiches") | ||
| .append("borough", "Brooklyn") | ||
| .append("restaurant_id", "5678")), | ||
| new UpdateManyModel<>(eq("name", "Mongo's Deli"), | ||
| set("cuisine", "Sandwiches and Salads")), | ||
| new DeleteOneModel<>(eq("restaurant_id", "1234")))); | ||
|
|
||
| BulkWriteResult bulkResult = Mono.from(bulkWritePublisher).block(); | ||
|
|
||
| System.out.printf(bulkResult.toString()); | ||
|
||
| // end-bulk-write-mixed | ||
|
|
||
| // start-bulk-write-unordered | ||
| Publisher<BulkWriteResult> bulkWritePublisher = restaurants.bulkWrite( | ||
| Arrays.asList(new InsertOneModel<>( | ||
| new Document("name", "Mongo's Deli") | ||
| .append("cuisine", "Sandwiches") | ||
| .append("borough", "Manhattan") | ||
| .append("restaurant_id", "1234")), | ||
| new InsertOneModel<>(new Document("name", "Mongo's Deli") | ||
| .append("cuisine", "Sandwiches") | ||
| .append("borough", "Brooklyn") | ||
| .append("restaurant_id", "5678")), | ||
| new UpdateManyModel<>(eq("name", "Mongo's Deli"), | ||
| set("cuisine", "Sandwiches and Salads")), | ||
| new DeleteOneModel<>(eq("restaurant_id", "1234"))), | ||
| new BulkWriteOptions().ordered(false)); | ||
|
|
||
| BulkWriteResult bulkResult = Mono.from(bulkWritePublisher).block(); | ||
|
|
||
| System.out.printf(bulkResult.toString()); | ||
| // end-bulk-write-unordered | ||
|
|
||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example seems a little artificial, as it replaces the whole document you'd lose the
restaurant_idis that desired?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will tweak the example to something in line with the example in the server manual.