BAEL-5359: Updating Multiple Fields in a MongoDB Document (#11861)

* BAEL-5359: Updating Multiple Fields in a MongoDB Document

* BAEL-5359: Add test class file

* BAEL-5359: Rename UpdateMultipleFieldsUnitTest.java to UpdateMultipleFieldsLiveTest.java
This commit is contained in:
Kapil Khandelwal 2022-02-28 01:51:17 +05:30 committed by GitHub
parent 0ddf9b6e38
commit eae57f5496
3 changed files with 160 additions and 0 deletions

View File

@ -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<Document> 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());
}
}

View File

@ -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<Document> 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());
}
}

View File

@ -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<Document> 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);
}
}