update document in mongodb using java:
Mongodb driver provides functionality to update document in mongodb using java. Update is a process in which single or multiple documents can be updated based on certain criteria. Let us see what javadoc says about update
12 <a title="class in com.mongodb.client.result" href="http://api.mongodb.org/java/3.0/com/mongodb/client/result/UpdateResult.html">UpdateResult</a> updateOne(<a title="interface in org.bson.conversions" href="http://api.mongodb.org/java/3.0/org/bson/conversions/Bson.html">Bson</a> filter,<a title="interface in org.bson.conversions" href="http://api.mongodb.org/java/3.0/org/bson/conversions/Bson.html">Bson</a> update)Update a single document in the collection according to the specified arguments.
- Parameters:
filter
– a document describing the query filter, which may not be null.update
– a document describing the update, which may not be null. The update to apply must include only update operators.- Returns:
- the result of the update one operation
Let us try to understand with an example. To update document in mongodb using java, consider we have below document in collection.
1
2
3
4
5
6
|
{
"_id" : ObjectId("55daa2f7e60dd21204306b77"),
"name" : "Harish Taware",
"salary" : 40000,
"type" : "FT"
}
|
Now we want to update the salary to 80000. We have to provide
- A document which will identify above document. Let us say we want to update salary where name is “Harish Taware”
- A document which will specify the “$set” operation and the value which will itself be a Document.
Consider below code
1
2
3
4
|
Bson filter = new Document("name", "Harish Taware");
Bson newValue = new Document("salary", 90000);
Bson updateOperationDocument = new Document("$set", newValue);
collection.updateOne(filter, updateOperationDocument);
|
Here,
- filter variable stores the document with name Harish Taware
- newValue is the document which specifies salary needs to be updated to 90000
- updateOperationDocument specifies that a set operation is to be performed.
- collection.updateOne(filter,updateOperationDocument) actually does the job of updating document.
Here is complete code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.thejavageek.mongodb;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class MongodbFind {
public static void main(String[] args) {
MongoClient client = new MongoClient("localhost", 27017);
MongoDatabase database = client.getDatabase("employee_db");
MongoCollection<Document> collection = database
.getCollection("employees");
Bson filter = new Document("name", "Harish Taware");
Bson newValue = new Document("salary", 90000);
Bson updateOperationDocument = new Document("$set", newValue);
collection.updateOne(filter, updateOperationDocument);
client.close();
}
}
|
Run the program to update mongodb document using java. Notice the salary is changed
1
2
3
4
5
6
|
{
"_id" : ObjectId("55daa2f7e60dd21204306b77"),
"name" : "Harish Taware",
"salary" : 90000,
"type" : "FT"
}
|
Just like updateOne, we have updateMany() method to update multiple documents at once. I hope the article helped understand how to update document in mongodb using java.