import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query;
import static org.springframework.data.mongodb.core.query.Update;
...
WriteResult wr = mongoTemplate.updateMulti(
new Query(where("empAge").is(24)),new Update().inc("salary", 50.00),Employee.class);
Methods for executing updates for documents:public static Update update(String key, Object value) and using static imports.Update addToSet (String key, Object value) Update using the $addToSet update modifierUpdate inc (String key, Number inc) Update using the $inc update modifierUpdate pop (String key, Update.Position pos) Update using the $pop update modifierUpdate pull (String key, Object value) Update using the $pull update modifierUpdate pullAll (String key, Object[] values) Update using the $pullAll update modifierUpdate push (String key, Object value) Update using the $push update modifierUpdate pushAll (String key, Object[] values) Update using the $pushAll update modifierUpdate rename (String oldName, String newName) Update using the $rename update modifierUpdate set (String key, Object value) Update using the $set update modifierUpdate unset (String key) Update using the $unset update modifierEmployee employee = new Employee("...");
//update employee object into entity collection
mongoOperation.save(employee);
//update employee object into "newCollection" collection
mongoOperation.save("newCollection",employee);
//update first found record, empName field, where empId = 1004,
//from your default collection
mongoOperation.updateFirst(
new Query(Criteria.where("empId").is(1004)),
Update.update("empName", "new name"));
//update first found record, empName field, where empId = 1004,
//from collection named "employee"
mongoOperation.updateFirst("employee",
new Query(Criteria.where("empId").is(1004)),
Update.update("empName", "new name"));
//update all found records, salary field, where empAge = "24",
//from collection named "employee"
mongoOperation.updateMulti("employee",
new Query(Criteria.where("empAge").is(24)),
Update.update("salary", 70000));
//update first found record, age field, where empId = 1001, using $inc
Update updateAge = new Update();
updateAge.inc("age", 10);
mongoOperation.updateFirst("employee",
new Query(Criteria.where("empId").is(1001)), updateAge);

package com.dineshonjava.mongo.dto;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author Dinesh Rajput
*
*/
@Document
public class Employee {
@Id
private int empId;
private String empName;
private long salary;
private int empAge;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public int getEmpAge() {
return empAge;
}
public void setEmpAge(int empAge) {
this.empAge = empAge;
}
@Override
public String toString() {
return "Employee [age=" + empAge + ", empName=" + empName + ", empId="
+ empId + ", salary=" + salary + "]";
}
}
<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
<!-- Default bean name is 'mongo' -->
<mongo:mongo host="localhost" port="27017"/>
<!-- Default bean name is 'mongo' -->
<mongo:mongo>
<mongo:options connections-per-host="100"
threads-allowed-to-block-for-connection-multiplier="5"
max-wait-time="120000000"
connect-timeout="10000000"
socket-keep-alive="true"
socket-timeout="15000000"
auto-connect-retry="true"/>
</mongo:mongo>
<context:annotation-config/>
<context:component-scan base-package="com.dineshonjava.mongo">
<context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>
</context:component-scan>
<!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->
<bean class="org.springframework.data.mongodb.core.MongoTemplate" id="mongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="dineshonjavaDB"/>
</bean>
</beans>
package com.dineshonjava.mongo.main;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import com.dineshonjava.mongo.dto.Employee;
/**
* @author Dinesh Rajput
*
*/
@Repository
public class HelloMongoDB {
@Autowired
MongoOperations mongoOperations;
public void execute() {
if (mongoOperations.collectionExists(Employee.class)) {
mongoOperations.dropCollection(Employee.class);
}
// Case1 - insert a employee, put "DOJ" as collection name
Employee employee = new Employee();
employee.setEmpId(1001);
employee.setEmpName("Anamika Rajput");
employee.setSalary(30000);
employee.setEmpAge(23);
mongoOperations.save(employee, "DOJ");
// find
Employee employee1 = mongoOperations.findOne(query(where("empId").is(1001)), Employee.class,"DOJ");
System.out.println(employee1);
// Update employee object, salary
employee1.setSalary(35000);
mongoOperations.save(employee1, "DOJ");
// find
Employee employeeupdated = mongoOperations.findOne(query(where("empId").is(1001)), Employee.class,"DOJ");
System.out.println(employeeupdated);
// Case 2 ... update salary field, $set
Employee employee2 = mongoOperations.findOne(query(where("empId").is(1001)), Employee.class,"orders");
System.out.println(employee2);
// Update employee object, salary
System.out.println("Case 2...by updateFirst() - $set");
mongoOperations.updateFirst(query(where("empId").is(1001)), Update.update("salary", 40000),"orders");
// find
Employee employeeupdated1 = mongoOperations.findOne(query(where("empId").is(1001)), Employee.class,"orders");
System.out.println(employeeupdated1);
// Case 3 ... update salary field, $inc
System.out.println("Case 3...by updateFirst() - $inc");
Update updateSalary = new Update();
updateSalary.inc("salary", 1000);
mongoOperations.updateFirst(query(where("empId").is(1001)), updateSalary, "orders");
Employee employee3 = mongoOperations.findOne(query(where("empId").is(1001)), Employee.class);
System.out.println(employee3);
}
}
package com.dineshonjava.mongo.main;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Dinesh Rajput
*
*/
public class HelloMongoTestApp {
/**
* @param args
*/
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("mongo-config.xml");
HelloMongoDB hello = (HelloMongoDB) context.getBean("helloMongoDB");
hello.execute();
System.out.println( "DONE!" );
}
}
Labels: mongodb