diff --git a/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/update/MultipleFieldsExample.java b/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/update/MultipleFieldsExample.java index b2fcddeafb..ebc56cbfd0 100644 --- a/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/update/MultipleFieldsExample.java +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/update/MultipleFieldsExample.java @@ -10,35 +10,35 @@ import com.mongodb.client.result.UpdateResult; public class MultipleFieldsExample { - public static void main(String[] args) { + public static void main(String[] args) { - // - // Connect to cluster (default is localhost:27017) - // + // + // Connect to cluster (default is localhost:27017) + // - MongoClient mongoClient = new MongoClient("localhost", 27017); - MongoDatabase database = mongoClient.getDatabase("baeldung"); - MongoCollection collection = database.getCollection("employee"); + MongoClient mongoClient = new MongoClient("localhost", 27017); + MongoDatabase database = mongoClient.getDatabase("baeldung"); + MongoCollection collection = database.getCollection("employee"); - // - // Filter on the basis of employee_id - // + // + // Filter on the basis of employee_id + // - BasicDBObject searchQuery = new BasicDBObject("employee_id", 794875); + BasicDBObject searchQuery = new BasicDBObject("employee_id", 794875); - // - // Update the fields in Document - // + // + // Update the fields in Document + // - BasicDBObject updateFields = new BasicDBObject(); - updateFields.append("department_id", 3); - updateFields.append("job", "Sales Manager"); - BasicDBObject setQuery = new BasicDBObject(); - setQuery.append("$set", updateFields); - UpdateResult updateResult = collection.updateMany(searchQuery, setQuery); + BasicDBObject updateFields = new BasicDBObject(); + updateFields.append("department_id", 3); + updateFields.append("job", "Sales Manager"); + BasicDBObject setQuery = new BasicDBObject(); + setQuery.append("$set", updateFields); + UpdateResult updateResult = collection.updateMany(searchQuery, setQuery); - System.out.println("updateResult:- " + updateResult); - System.out.println("updateResult:- " + updateResult.getModifiedCount()); + System.out.println("updateResult:- " + updateResult); + System.out.println("updateResult:- " + updateResult.getModifiedCount()); - } + } } diff --git a/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/update/UpdateMultipleFields.java b/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/update/UpdateMultipleFields.java index 20af6d99cb..96dd086ed7 100644 --- a/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/update/UpdateMultipleFields.java +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/update/UpdateMultipleFields.java @@ -11,26 +11,25 @@ import com.mongodb.client.result.UpdateResult; public class UpdateMultipleFields { - public static void main(String[] args) { + public static void main(String[] args) { - // - // Connect to cluster - // + // + // Connect to cluster + // - MongoClient mongoClient = new MongoClient("localhost", 27007); - MongoDatabase database = mongoClient.getDatabase("baeldung"); - MongoCollection collection = database.getCollection("employee"); + MongoClient mongoClient = new MongoClient("localhost", 27007); + MongoDatabase database = mongoClient.getDatabase("baeldung"); + MongoCollection collection = database.getCollection("employee"); - // - // Update query - // + // + // Update query + // - UpdateResult updateResult = collection.updateMany(Filters.eq("employee_id", 794875), - Updates.combine(Updates.set("department_id", 4), Updates.set("job", "Sales Manager"))); + UpdateResult updateResult = collection.updateMany(Filters.eq("employee_id", 794875), Updates.combine(Updates.set("department_id", 4), Updates.set("job", "Sales Manager"))); - System.out.println("updateResult:- " + updateResult); - System.out.println("updateResult:- " + updateResult.getModifiedCount()); + System.out.println("updateResult:- " + updateResult); + System.out.println("updateResult:- " + updateResult.getModifiedCount()); - } + } -} \ No newline at end of file +} diff --git a/persistence-modules/java-mongodb/src/test/java/com/baeldung/update/UpdateMultipleFieldsLiveTest.java b/persistence-modules/java-mongodb/src/test/java/com/baeldung/update/UpdateMultipleFieldsLiveTest.java index d1538d5312..d06de23423 100644 --- a/persistence-modules/java-mongodb/src/test/java/com/baeldung/update/UpdateMultipleFieldsLiveTest.java +++ b/persistence-modules/java-mongodb/src/test/java/com/baeldung/update/UpdateMultipleFieldsLiveTest.java @@ -2,8 +2,6 @@ package com.baeldung.update; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; import org.bson.Document; import org.junit.Before; @@ -15,7 +13,6 @@ import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Filters; import com.mongodb.client.model.Updates; -import com.mongodb.client.result.UpdateResult; public class UpdateMultipleFieldsLiveTest { @@ -30,8 +27,7 @@ public class UpdateMultipleFieldsLiveTest { db = mongoClient.getDatabase("baeldung"); collection = db.getCollection("employee"); - collection.insertOne(Document.parse( - "{'employee_id':794875,'employee_name': 'David smith','job': 'Sales Representative','department_id': 2,'salary': 20000,'hire_date': NumberLong(\"1643969311817\")}")); + collection.insertOne(Document.parse("{'employee_id':794875,'employee_name': 'David Smith','job': 'Sales Representative','department_id': 2,'salary': 20000,'hire_date': NumberLong(\"1643969311817\")}")); } } @@ -47,7 +43,8 @@ public class UpdateMultipleFieldsLiveTest { collection.updateMany(searchQuery, setQuery); - Document nameDoc = collection.find(Filters.eq("employee_id", 794875)).first(); + Document nameDoc = collection.find(Filters.eq("employee_id", 794875)) + .first(); assertNotNull(nameDoc); assertFalse(nameDoc.isEmpty()); @@ -62,10 +59,10 @@ public class UpdateMultipleFieldsLiveTest { @Test public void updateMultipleFieldsUsingDocument() { - collection.updateMany(Filters.eq("employee_id", 794875), - Updates.combine(Updates.set("department_id", 4), Updates.set("job", "Sales Manager"))); + collection.updateMany(Filters.eq("employee_id", 794875), Updates.combine(Updates.set("department_id", 4), Updates.set("job", "Sales Manager"))); - Document nameDoc = collection.find(Filters.eq("employee_id", 794875)).first(); + Document nameDoc = collection.find(Filters.eq("employee_id", 794875)) + .first(); assertNotNull(nameDoc); assertFalse(nameDoc.isEmpty()); @@ -78,3 +75,4 @@ public class UpdateMultipleFieldsLiveTest { } } +