Spring Data MongoDB : Update document
Published: May 18, 2011 , Updated: May 18, 2011 , Author: mkyong
In Spring data for MongoDB, you can use save(), updateFirst() and updateMulti() to update existing domain object from mongoDB database.
User user = new User("..."); //update user object into entity collection mongoOperation.save(user); //update user object into "new collection" collection mongoOperation.save("new collection",user); //update first found record, firstname field, where _id = 1000, //from your default collection mongoOperation.updateFirst( new Query(Criteria.where("_id").is("1000")), Update.update("firstname", "new first name")); //update first found record, firstname field, where _id = 1000, //from collection named "user" mongoOperation.updateFirst("user", new Query(Criteria.where("_id").is("1000")), Update.update("firstname", "new first name")); //update all found records, age field, where firstname = "yong", //from collection named "user" mongoOperation.updateMulti("user", new Query(Criteria.where("firstname").is("yong")), Update.update("age", 40)); //update first found record, age field, where id = "1000", using $inc Update updateAge = new Update(); updateAge.inc("age", 10); mongoOperation.updateFirst("user", new Query(Criteria.where("_id").is("1000")), updateAge);
By default, if you didn’t define the collection name in updateFirst or updateMulti method, it will update the found object in default collection.
updateFirst or updateMulti?
- “updateFirst” is means, updates the first object that is found.
- “updateMulti” is means, updates all objects that are found.
MUST READ
You must read this Spring’s mongodb update documentation, to study more overloaded update() methods.
You must read this Spring’s mongodb update documentation, to study more overloaded update() methods.
Update documents example
Full example to show the use of “Spring Data MongoDB” APIs to update user object to MongoDB.
public class User { private String id; private String firstname; private String lastname; private int age; //getter, setters and constructors }
package com.mkyong.core; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.data.document.mongodb.MongoOperations; import org.springframework.data.document.mongodb.query.Criteria; import org.springframework.data.document.mongodb.query.Query; import org.springframework.data.document.mongodb.query.Update; import com.mkyong.config.SpringMongoConfig; import com.mkyong.user.User; public class App { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext( SpringMongoConfig.class); MongoOperations mongoOperation = (MongoOperations) ctx .getBean("mongoTemplate"); User user = new User("1000", "user-first", "user-last", 17); // Case 1 ... update object System.out.println("Case 1...by save()"); // Save user object mongoOperation.save(user); User userPrint1 = mongoOperation.findOne(new Query(Criteria.where("id") .is("1000")), User.class); System.out.println(userPrint1); // Update user object, lastname user.setLastname("new last name"); mongoOperation.save(user); User userPrint2 = mongoOperation.findOne(new Query(Criteria.where("id") .is("1000")), User.class); System.out.println(userPrint2); // Case 2 ... update firstname field, $set System.out.println("Case 2...by updateFirst() - $set"); mongoOperation.updateFirst("user", new Query(Criteria.where("_id").is("1000")), Update.update("firstname", "new first name")); User userPrint3 = mongoOperation.findOne(new Query(Criteria.where("id") .is("1000")), User.class); System.out.println(userPrint3); // Case 3 ... update age field, $inc System.out.println("Case 3...by updateFirst() - $inc"); Update updateAge = new Update(); updateAge.inc("age", 10); mongoOperation.updateFirst("user", new Query(Criteria.where("_id").is("1000")), updateAge); User userPrint4 = mongoOperation.findOne(new Query(Criteria .where("_id").is("1000")), User.class); System.out.println(userPrint4); } }
Output
Case 1...by save() User [id=1000, firstname=user-first, lastname=user-last, age=17] User [id=1000, firstname=user-first, lastname=new last name, age=17] Case 2...by updateFirst() - $set User [id=1000, firstname=new first name, lastname=new last name, age=17] Case 3...by updateFirst() - $inc User [id=1000, firstname=new first name, lastname=new last name, age=27]
Download it – SpringData-MongoDB-Update-Document-Example.zip (7 KB)
References
Note : You can find more similar articles at - Java MongoDB Tutorials






