From ffe4390ed884d2e91826de087887ff47c4ad48e2 Mon Sep 17 00:00:00 2001 From: Kapil Khandelwal Date: Thu, 24 Mar 2022 02:40:32 +0530 Subject: [PATCH] BAEL-5398: Bulk Update of Documents in MongoDB with Java (#11963) --- .../baeldung/mongo/update/BulkOperations.java | 82 +++++++++++++++++ .../baeldung/mongo/BulkOperationLiveTest.java | 92 +++++++++++++++++++ .../src/test/resources/populations.json | 4 + 3 files changed, 178 insertions(+) create mode 100644 persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/update/BulkOperations.java create mode 100644 persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/BulkOperationLiveTest.java create mode 100644 persistence-modules/java-mongodb-2/src/test/resources/populations.json diff --git a/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/update/BulkOperations.java b/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/update/BulkOperations.java new file mode 100644 index 0000000000..78ab8f20da --- /dev/null +++ b/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/update/BulkOperations.java @@ -0,0 +1,82 @@ +package com.baeldung; + +import java.util.ArrayList; +import java.util.List; + +import org.bson.Document; + +import com.mongodb.MongoClient; +import com.mongodb.bulk.BulkWriteResult; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.DeleteOneModel; +import com.mongodb.client.model.InsertOneModel; +import com.mongodb.client.model.ReplaceOneModel; +import com.mongodb.client.model.UpdateManyModel; +import com.mongodb.client.model.UpdateOneModel; +import com.mongodb.client.model.WriteModel; + +public class BulkOperations { + + private static MongoClient mongoClient; + private static MongoDatabase database; + private static MongoCollection collection; + private static String testCollectionName; + private static String databaseName; + + public static void setUp() { + + databaseName = "baeldung"; + testCollectionName = "populations"; + if (mongoClient == null) { + mongoClient = new MongoClient("localhost", 27017); + database = mongoClient.getDatabase(databaseName); + collection = database.getCollection(testCollectionName); + + } + + } + + public static void bulkOperations() { + + List> writeOperations = new ArrayList>(); + writeOperations.add(new InsertOneModel(new Document("cityId", 1128).append("cityName", "Kathmandu") + .append("countryName", "Nepal") + .append("continentName", "Asia") + .append("population", 12))); + writeOperations.add(new InsertOneModel(new Document("cityId", 1130).append("cityName", "Mumbai") + .append("countryName", "India") + .append("continentName", "Asia") + .append("population", 55))); + writeOperations.add(new UpdateOneModel(new Document("cityName", "New Delhi"), // filter to update document + new Document("$set", new Document("status", "High Population")) // update + )); + writeOperations.add(new UpdateManyModel(new Document("cityName", "London"), // filter to update multiple documents + new Document("$set", new Document("status", "Low Population")) // update + )); + writeOperations.add(new DeleteOneModel(new Document("cityName", "Mexico City"))); + writeOperations.add(new ReplaceOneModel(new Document("cityName", "New York"), new Document("cityId", 1124).append("cityName", "New York") + .append("countryName", "United States") + .append("continentName", "North America") + .append("population", 28))); + + BulkWriteResult bulkWriteResult = collection.bulkWrite(writeOperations); + System.out.println("bulkWriteResult:- " + bulkWriteResult); + } + + public static void main(String args[]) { + + // + // Connect to cluster (default is localhost:27017) + // + setUp(); + + // + // Bulk operations + // + + bulkOperations(); + + } +} + diff --git a/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/BulkOperationLiveTest.java b/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/BulkOperationLiveTest.java new file mode 100644 index 0000000000..8b7029a2df --- /dev/null +++ b/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/BulkOperationLiveTest.java @@ -0,0 +1,92 @@ +package com.baeldung.mongo; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +import org.bson.Document; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.mongodb.BasicDBObject; +import com.mongodb.DBObject; +import com.mongodb.MongoClient; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.DeleteOneModel; +import com.mongodb.client.model.Filters; +import com.mongodb.client.model.InsertOneModel; +import com.mongodb.client.model.ReplaceOneModel; +import com.mongodb.client.model.UpdateManyModel; +import com.mongodb.client.model.UpdateOneModel; +import com.mongodb.client.model.WriteModel; +import com.mongodb.client.result.UpdateResult; + +public class BulkOperationLiveTest { + + private static MongoClient mongoClient; + private static MongoDatabase db; + private static MongoCollection collection; + private static final String DATASET_JSON = "/populations.json"; + + @BeforeClass + public static void setup() throws IOException { + if (mongoClient == null) { + mongoClient = new MongoClient("localhost", 27017); + db = mongoClient.getDatabase("baeldung"); + collection = db.getCollection("populations"); + collection.drop(); + + InputStream is = BulkOperationLiveTest.class.getResourceAsStream(DATASET_JSON); + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + reader.lines() + .forEach(line -> collection.insertOne(Document.parse(line))); + reader.close(); + } + } + + @Test + public void givenPopulationCollection_whenBulkOperations_thenCheckingForDocument() { + + List> writeOperations = new ArrayList>(); + writeOperations.add(new InsertOneModel(new Document("cityId", 1128).append("cityName", "Kathmandu") + .append("countryName", "Nepal") + .append("continentName", "Asia") + .append("population", 12))); + writeOperations.add(new InsertOneModel(new Document("cityId", 1130).append("cityName", "Mumbai") + .append("countryName", "India") + .append("continentName", "Asia") + .append("population", 55))); + writeOperations.add(new UpdateOneModel(new Document("cityName", "New Delhi"), // filter to update document + new Document("$set", new Document("status", "High Population")) // update + )); + writeOperations.add(new UpdateManyModel(new Document("cityName", "London"), // filter to update multiple documents + new Document("$set", new Document("status", "Low Population")) // update + )); + writeOperations.add(new DeleteOneModel(new Document("cityName", "Mexico City"))); + writeOperations.add(new ReplaceOneModel(new Document("cityName", "New York"), new Document("cityId", 1124).append("cityName", "New York") + .append("countryName", "United States") + .append("continentName", "North America") + .append("population", 28))); + + Document orderDetail = collection.find(Filters.eq("cityId", 1124)) + .first(); + assertNotNull(orderDetail); + assertFalse(orderDetail.isEmpty()); + + } + + @AfterClass + public static void cleanUp() { + mongoClient.close(); + } + +} + diff --git a/persistence-modules/java-mongodb-2/src/test/resources/populations.json b/persistence-modules/java-mongodb-2/src/test/resources/populations.json new file mode 100644 index 0000000000..200daa206b --- /dev/null +++ b/persistence-modules/java-mongodb-2/src/test/resources/populations.json @@ -0,0 +1,4 @@ +{ "cityId":1124, "cityName":"New York", "countryName":"United States","continentName":"North America","population":22 } +{ "cityId":1125, "cityName":"Mexico City", "countryName":"Mexico", "continentName":"North America","population":25 } +{ "cityId":1126, "cityName":"New Delhi", "countryName":"India","continentName":"Asia","population":45 } +{ "cityId":1134, "cityName":"London", "countryName":"England", "continentName":"Europe", "population":32 }