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 new file mode 100644 index 0000000000..b2fcddeafb --- /dev/null +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/update/MultipleFieldsExample.java @@ -0,0 +1,44 @@ +package com.baeldung.mongo.update; + +import org.bson.Document; + +import com.mongodb.BasicDBObject; +import com.mongodb.MongoClient; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.result.UpdateResult; + +public class MultipleFieldsExample { + + public static void main(String[] args) { + + // + // Connect to cluster (default is localhost:27017) + // + + MongoClient mongoClient = new MongoClient("localhost", 27017); + MongoDatabase database = mongoClient.getDatabase("baeldung"); + MongoCollection collection = database.getCollection("employee"); + + // + // Filter on the basis of employee_id + // + + BasicDBObject searchQuery = new BasicDBObject("employee_id", 794875); + + // + // 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); + + 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 new file mode 100644 index 0000000000..20af6d99cb --- /dev/null +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/update/UpdateMultipleFields.java @@ -0,0 +1,36 @@ +package com.baeldung.mongo.update; + +import org.bson.Document; + +import com.mongodb.MongoClient; +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 UpdateMultipleFields { + + public static void main(String[] args) { + + // + // Connect to cluster + // + + MongoClient mongoClient = new MongoClient("localhost", 27007); + MongoDatabase database = mongoClient.getDatabase("baeldung"); + MongoCollection collection = database.getCollection("employee"); + + // + // Update query + // + + 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()); + + } + +} \ 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 new file mode 100644 index 0000000000..d1538d5312 --- /dev/null +++ b/persistence-modules/java-mongodb/src/test/java/com/baeldung/update/UpdateMultipleFieldsLiveTest.java @@ -0,0 +1,80 @@ +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; +import org.junit.Test; + +import com.mongodb.BasicDBObject; +import com.mongodb.MongoClient; +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 { + + private MongoClient mongoClient; + private MongoDatabase db; + private MongoCollection collection; + + @Before + public void setup() { + if (mongoClient == null) { + mongoClient = new MongoClient("localhost", 27017); + 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\")}")); + } + } + + @Test + public void updateMultipleFieldsUsingDBObject() { + + BasicDBObject searchQuery = new BasicDBObject("employee_id", 794875); + BasicDBObject updateFields = new BasicDBObject(); + updateFields.append("department_id", 4); + updateFields.append("job", "Sales Manager"); + BasicDBObject setQuery = new BasicDBObject(); + setQuery.append("$set", updateFields); + + collection.updateMany(searchQuery, setQuery); + + Document nameDoc = collection.find(Filters.eq("employee_id", 794875)).first(); + assertNotNull(nameDoc); + assertFalse(nameDoc.isEmpty()); + + String job = nameDoc.get("job", String.class); + assertNotNull(job); + + Integer department_id = nameDoc.get("department_id", Integer.class); + assertNotNull(department_id); + + } + + @Test + public void updateMultipleFieldsUsingDocument() { + + 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(); + assertNotNull(nameDoc); + assertFalse(nameDoc.isEmpty()); + + String job = nameDoc.get("job", String.class); + assertNotNull(job); + + Integer department_id = nameDoc.get("department_id", Integer.class); + assertNotNull(department_id); + + } + +}