
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to update an existing document in MongoDB collection using Java?
The update() method updates the values in the existing document.
Syntax
db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)
In Java, you can update a single document using the updateOne()method of the com.mongodb.client.MongoCollection interface. To this method, you need to pass the filter and values for the update.
Example
import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Filters; import com.mongodb.client.model.Updates; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class UpdatingDocuments { public static void main( String args[] ) { // Creating a Mongo client MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Connecting to the database MongoDatabase database = mongo.getDatabase("myDatabase"); //Creating a collection object MongoCollection<Document>collection = database.getCollection("myCollection"); //Preparing documents Document document1 = new Document("name", "Ram").append("age", 26).append("city", "Hyderabad"); Document document2 = new Document("name", "Robert").append("age", 27).append("city", "Vishakhapatnam"); Document document3 = new Document("name", "Rhim").append("age", 30).append("city", "Delhi"); //Inserting the created documents List<Document> list = new ArrayList<Document>(); list.add(document1); list.add(document2); list.add(document3); collection.insertMany(list); //Updating a document collection.updateOne(Filters.eq("name", "Robert"), Updates.set("city", "Delhi")); System.out.println("Document update successfully..."); FindIterable<Document> iterDoc = collection.find(); Iterator it = iterDoc.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }
Output
Document update successfully... Document after update: Document{{_id=5e86dd21e9b25f3460b1abe0, name=Ram, age=26, city=Hyderabad}} Document{{_id=5e86dd21e9b25f3460b1abe1, name=Robert, age=27, city=Vishakhapatnam}} Document{{_id=5e86dd21e9b25f3460b1abe2, name=Rhim, age=30, city=Delhi}}
- Related Articles
- How to update a MongoDB document without overwriting the existing one?
- How to Update multiple documents in a MongoDB collection using Java?
- How to update MongoDB collection using $toLower?
- How to insert a document into a MongoDB collection using Java?
- How to insert multiple document into a MongoDB collection using Java?
- How to update or modify the existing documents of a collection in MongoDB?
- MongoDB query to update a specific document from a collection
- How to delete document from a collection in MongoDB using deleteOne() method?
- Check for Existing Document in MongoDB?
- Invoke convertToCapped and convert an existing collection to capped in MongoDB
- Removing an array element from MongoDB collection using update() and $pull
- How can we update an existing JSON data using javax.json API in Java?
- MongoDB query to update nested document
- MongoDB query to add a document in an already created collection
- How to delete a MongoDB document using Java?

Advertisements