BAEL-5401: Update Fields of Documents in MongoDB Using the Java Driver (#11889)

This commit is contained in:
Kapil Khandelwal 2022-03-06 09:59:16 +05:30 committed by GitHub
parent 28519a494c
commit dafb4d1f58
2 changed files with 271 additions and 0 deletions

View File

@ -0,0 +1,128 @@
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.FindOneAndReplaceOptions;
import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.mongodb.client.model.ReturnDocument;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.UpdateResult;
public class UpdateFields {
private static MongoClient mongoClient;
private static MongoDatabase database;
private static MongoCollection<Document> collection;
public static void updateOne() {
UpdateResult updateResult = collection.updateOne(Filters.eq("student_name", "Paul Starc"), Updates.set("address", "Hostel 2"));
System.out.println("updateResult:- " + updateResult);
}
public static void updateMany() {
UpdateResult updateResult = collection.updateMany(Filters.lt("age", 20), Updates.set("Review", true));
System.out.println("updateResult:- " + updateResult);
}
public static void replaceOne() {
Document replaceDocument = new Document();
replaceDocument.append("student_id", 8764)
.append("student_name", "Paul Starc")
.append("address", "Hostel 3")
.append("age", 18)
.append("roll_no", 199406);
UpdateResult updateResult = collection.replaceOne(Filters.eq("student_id", 8764), replaceDocument);
System.out.println("updateResult:- " + updateResult);
}
public static void findOneAndReplace() {
Document replaceDocument = new Document();
replaceDocument.append("student_id", 8764)
.append("student_name", "Paul Starc")
.append("address", "Hostel 4")
.append("age", 18)
.append("roll_no", 199406);
Document sort = new Document("roll_no", 1);
Document projection = new Document("_id", 0).append("student_id", 1)
.append("address", 1);
Document resultDocument = collection.findOneAndReplace(Filters.eq("student_id", 8764), replaceDocument, new FindOneAndReplaceOptions().upsert(true)
.sort(sort)
.projection(projection)
.returnDocument(ReturnDocument.AFTER));
System.out.println("resultDocument:- " + resultDocument);
}
public static void findOneAndUpdate() {
Document sort = new Document("roll_no", 1);
Document projection = new Document("_id", 0).append("student_id", 1)
.append("address", 1);
Document resultDocument = collection.findOneAndUpdate(Filters.eq("student_id", 8764), Updates.inc("roll_no", 5), new FindOneAndUpdateOptions().upsert(true)
.sort(sort)
.projection(projection)
.returnDocument(ReturnDocument.AFTER));
System.out.println("resultDocument:- " + resultDocument);
}
public static void setup() {
if (mongoClient == null) {
mongoClient = new MongoClient("localhost", 27017);
database = mongoClient.getDatabase("baeldung");
collection = database.getCollection("student");
}
}
public static void main(String[] args) {
//
// Connect to cluster (default is localhost:27017)
//
setup();
//
// Update a document using updateOne method
//
updateOne();
//
// Update documents using updateMany method
//
updateMany();
//
// replace a document using replaceOne method
//
replaceOne();
//
// replace a document using findOneAndReplace method
//
findOneAndReplace();
//
// Update a document using findOneAndUpdate method
//
findOneAndUpdate();
}
}

View File

@ -0,0 +1,143 @@
package com.baeldung.update;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import org.bson.Document;
import org.junit.BeforeClass;
import org.junit.Test;
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.FindOneAndReplaceOptions;
import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.mongodb.client.model.ReturnDocument;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.UpdateResult;
public class UpdateFieldLiveTest {
private static MongoClient mongoClient;
private static MongoDatabase db;
private static MongoCollection<Document> collection;
@BeforeClass
public static void setup() {
if (mongoClient == null) {
mongoClient = new MongoClient("localhost", 27017);
db = mongoClient.getDatabase("baeldung");
collection = db.getCollection("student");
collection.insertOne(Document.parse("{ \"student_id\": 8764,\"student_name\": \"Paul Starc\",\"address\": \"Hostel 1\",\"age\": 16,\"roll_no\":199406}"));
}
}
@Test
public void updateOne() {
UpdateResult updateResult = collection.updateOne(Filters.eq("student_name", "Paul Starc"), Updates.set("address", "Hostel 2"));
Document studentDetail = collection.find(Filters.eq("student_name", "Paul Starc"))
.first();
assertNotNull(studentDetail);
assertFalse(studentDetail.isEmpty());
String address = studentDetail.getString("address");
String expectedAdderess = "Hostel 2";
assertEquals(expectedAdderess, address);
}
@Test
public void updateMany() {
UpdateResult updateResult = collection.updateMany(Filters.lt("age", 20), Updates.set("Review", true));
Document studentDetail = collection.find(Filters.eq("student_name", "Paul Starc"))
.first();
assertNotNull(studentDetail);
assertFalse(studentDetail.isEmpty());
Boolean review = studentDetail.getBoolean("Review");
Boolean expectedAdderess = true;
assertEquals(expectedAdderess, review);
}
@Test
public void replaceOne() {
Document replaceDocument = new Document();
replaceDocument.append("student_id", 8764)
.append("student_name", "Paul Starc")
.append("address", "Hostel 3")
.append("age", 18)
.append("roll_no", 199406);
UpdateResult updateResult = collection.replaceOne(Filters.eq("student_id", 8764), replaceDocument);
Document studentDetail = collection.find(Filters.eq("student_name", "Paul Starc"))
.first();
assertNotNull(studentDetail);
assertFalse(studentDetail.isEmpty());
Integer age = studentDetail.getInteger("age");
Integer expectedAge = 18;
assertEquals(expectedAge, age);
}
@Test
public void findOneAndReplace() {
Document replaceDocument = new Document();
replaceDocument.append("student_id", 8764)
.append("student_name", "Paul Starc")
.append("address", "Hostel 4")
.append("age", 18)
.append("roll_no", 199406);
Document sort = new Document("roll_no", 1);
Document projection = new Document("_id", 0).append("student_id", 1)
.append("address", 1);
Document resultDocument = collection.findOneAndReplace(Filters.eq("student_id", 8764), replaceDocument, new FindOneAndReplaceOptions().upsert(true)
.sort(sort)
.projection(projection)
.returnDocument(ReturnDocument.AFTER));
Document studentDetail = collection.find(Filters.eq("student_name", "Paul Starc"))
.first();
assertNotNull(studentDetail);
assertFalse(studentDetail.isEmpty());
Integer age = studentDetail.getInteger("age");
Integer expectedAge = 18;
assertEquals(expectedAge, age);
}
@Test
public void findOneAndUpdate() {
Document sort = new Document("roll_no", 1);
Document projection = new Document("_id", 0).append("student_id", 1)
.append("address", 1);
Document resultDocument = collection.findOneAndUpdate(Filters.eq("student_id", 8764), Updates.inc("roll_no", 5), new FindOneAndUpdateOptions().upsert(true)
.sort(sort)
.projection(projection)
.returnDocument(ReturnDocument.AFTER));
Document studentDetail = collection.find(Filters.eq("student_name", "Paul Starc"))
.first();
assertNotNull(studentDetail);
assertFalse(studentDetail.isEmpty());
}
}