BAEL 5359: Update formatting (#11880)
* BAEL-5359: Updating Multiple Fields in a MongoDB Document * BAEL-5359: Add test class file * BAEL-5359: Rename UpdateMultipleFieldsUnitTest.java to UpdateMultipleFieldsLiveTest.java * BAEL-5359: Update formatting
This commit is contained in:
parent
2923b0cbb0
commit
2eb49b96cb
|
@ -10,35 +10,35 @@ import com.mongodb.client.result.UpdateResult;
|
||||||
|
|
||||||
public class MultipleFieldsExample {
|
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);
|
MongoClient mongoClient = new MongoClient("localhost", 27017);
|
||||||
MongoDatabase database = mongoClient.getDatabase("baeldung");
|
MongoDatabase database = mongoClient.getDatabase("baeldung");
|
||||||
MongoCollection<Document> collection = database.getCollection("employee");
|
MongoCollection<Document> 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();
|
BasicDBObject updateFields = new BasicDBObject();
|
||||||
updateFields.append("department_id", 3);
|
updateFields.append("department_id", 3);
|
||||||
updateFields.append("job", "Sales Manager");
|
updateFields.append("job", "Sales Manager");
|
||||||
BasicDBObject setQuery = new BasicDBObject();
|
BasicDBObject setQuery = new BasicDBObject();
|
||||||
setQuery.append("$set", updateFields);
|
setQuery.append("$set", updateFields);
|
||||||
UpdateResult updateResult = collection.updateMany(searchQuery, setQuery);
|
UpdateResult updateResult = collection.updateMany(searchQuery, setQuery);
|
||||||
|
|
||||||
System.out.println("updateResult:- " + updateResult);
|
System.out.println("updateResult:- " + updateResult);
|
||||||
System.out.println("updateResult:- " + updateResult.getModifiedCount());
|
System.out.println("updateResult:- " + updateResult.getModifiedCount());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,26 +11,25 @@ import com.mongodb.client.result.UpdateResult;
|
||||||
|
|
||||||
public class UpdateMultipleFields {
|
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);
|
MongoClient mongoClient = new MongoClient("localhost", 27007);
|
||||||
MongoDatabase database = mongoClient.getDatabase("baeldung");
|
MongoDatabase database = mongoClient.getDatabase("baeldung");
|
||||||
MongoCollection<Document> collection = database.getCollection("employee");
|
MongoCollection<Document> collection = database.getCollection("employee");
|
||||||
|
|
||||||
//
|
//
|
||||||
// Update query
|
// Update query
|
||||||
//
|
//
|
||||||
|
|
||||||
UpdateResult updateResult = collection.updateMany(Filters.eq("employee_id", 794875),
|
UpdateResult updateResult = collection.updateMany(Filters.eq("employee_id", 794875), Updates.combine(Updates.set("department_id", 4), Updates.set("job", "Sales Manager")));
|
||||||
Updates.combine(Updates.set("department_id", 4), Updates.set("job", "Sales Manager")));
|
|
||||||
|
|
||||||
System.out.println("updateResult:- " + updateResult);
|
System.out.println("updateResult:- " + updateResult);
|
||||||
System.out.println("updateResult:- " + updateResult.getModifiedCount());
|
System.out.println("updateResult:- " + updateResult.getModifiedCount());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,6 @@ package com.baeldung.update;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertSame;
|
|
||||||
|
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
@ -15,7 +13,6 @@ import com.mongodb.client.MongoCollection;
|
||||||
import com.mongodb.client.MongoDatabase;
|
import com.mongodb.client.MongoDatabase;
|
||||||
import com.mongodb.client.model.Filters;
|
import com.mongodb.client.model.Filters;
|
||||||
import com.mongodb.client.model.Updates;
|
import com.mongodb.client.model.Updates;
|
||||||
import com.mongodb.client.result.UpdateResult;
|
|
||||||
|
|
||||||
public class UpdateMultipleFieldsLiveTest {
|
public class UpdateMultipleFieldsLiveTest {
|
||||||
|
|
||||||
|
@ -30,8 +27,7 @@ public class UpdateMultipleFieldsLiveTest {
|
||||||
db = mongoClient.getDatabase("baeldung");
|
db = mongoClient.getDatabase("baeldung");
|
||||||
collection = db.getCollection("employee");
|
collection = db.getCollection("employee");
|
||||||
|
|
||||||
collection.insertOne(Document.parse(
|
collection.insertOne(Document.parse("{'employee_id':794875,'employee_name': 'David Smith','job': 'Sales Representative','department_id': 2,'salary': 20000,'hire_date': NumberLong(\"1643969311817\")}"));
|
||||||
"{'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);
|
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);
|
assertNotNull(nameDoc);
|
||||||
assertFalse(nameDoc.isEmpty());
|
assertFalse(nameDoc.isEmpty());
|
||||||
|
|
||||||
|
@ -62,10 +59,10 @@ public class UpdateMultipleFieldsLiveTest {
|
||||||
@Test
|
@Test
|
||||||
public void updateMultipleFieldsUsingDocument() {
|
public void updateMultipleFieldsUsingDocument() {
|
||||||
|
|
||||||
collection.updateMany(Filters.eq("employee_id", 794875),
|
collection.updateMany(Filters.eq("employee_id", 794875), Updates.combine(Updates.set("department_id", 4), Updates.set("job", "Sales Manager")));
|
||||||
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);
|
assertNotNull(nameDoc);
|
||||||
assertFalse(nameDoc.isEmpty());
|
assertFalse(nameDoc.isEmpty());
|
||||||
|
|
||||||
|
@ -78,3 +75,4 @@ public class UpdateMultipleFieldsLiveTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue