Criteria
class used to create queries by specifying the criteria.
Example
Query
query = new Query();
query.addCriteria(Criteria.where("firstName").is("Phalgun"));
To
Update user details where firstName is Phalgun
Step 1:
Create Query
instance.
Query
query = new Query();
query.addCriteria(Criteria.where("firstName").is("Phalgun"));
Step 2:
Create an instance of
Update class. Update is used to construct mongoDB update clauses.
Update
update = new Update();
update.set("firstName",
"Ram");
update.set("lastName",
"Gurram");
Step 3:
Use MongoTemplate to
update all the records that matches to given criteris.
MongoClient
mongoClient = new MongoClient(host, port);
MongoTemplate
mongoTemplate = new MongoTemplate(mongoClient, database);
mongoTemplate.updateMulti(query,
update, Employee.class);
Find
the below working application.
package com.sample.app.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document("employees")
public class Employee {
@Id
private String id;
private String firstName;
private String lastName;
public Employee() {}
public Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=")
.append(lastName).append("]");
return builder.toString();
}
}
EmployeeRepository.java
package com.sample.app.repository;
import java.util.List;
import java.util.stream.Stream;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;
import com.sample.app.entity.Employee;
@Repository
public interface EmployeeRepository extends MongoRepository<Employee, String> {
List<Employee> findByFirstName(String firstName);
List<Employee> findByLastName(String lastName);
List<Employee> findByFirstNameAndLastName(String firstName, String lastName);
@Query("{'firstName' : ?0}")
Stream<Employee> findAllByCustomQueryAndStream(String firstName);
}
application.properties
spring.data.mongodb.database=myorg spring.data.mongodb.port=27017 spring.data.mongodb.host=localhost logging.level.org.springframework.data=WARN
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springDataMongo</groupId>
<artifactId>springDataMongo</artifactId>
<version>1</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
</project>
App.java
package com.sample.app;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import com.mongodb.MongoClient;
import com.sample.app.entity.Employee;
import org.springframework.data.mongodb.core.query.*;
@SpringBootApplication
public class App {
@Value("${spring.data.mongodb.database}")
private String database;
@Value("${spring.data.mongodb.host}")
private String host;
@Value("${spring.data.mongodb.port}")
private int port;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
public void dropPreviousData() {
MongoClient mongoClient = new MongoClient(host, port);
MongoOperations mongoOps = new MongoTemplate(mongoClient, database);
mongoOps.dropCollection("employees");
}
@Bean
public CommandLineRunner demo() {
return (args) -> {
dropPreviousData();
MongoClient mongoClient = new MongoClient(host, port);
MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, database);
Employee emp1 = new Employee("Phalgun", "Garimella");
Employee emp2 = new Employee("Sankalp", "Dubey");
Employee emp3 = new Employee("Arpan", "Garimella");
Employee emp4 = new Employee("Phalgun", "Dubey");
mongoTemplate.save(emp1);
mongoTemplate.save(emp2);
mongoTemplate.save(emp3);
mongoTemplate.save(emp4);
System.out.println("**************************************");
List<Employee> emps = mongoTemplate.findAll(Employee.class);
for (Employee emp : emps) {
System.out.println(emp);
}
Query query = new Query();
query.addCriteria(Criteria.where("firstName").is("Phalgun"));
Update update = new Update();
update.set("firstName", "Ram");
update.set("lastName", "Gurram");
mongoTemplate.updateMulti(query, update, Employee.class);
System.out.println("\n**************************************");
emps = mongoTemplate.findAll(Employee.class);
for (Employee emp : emps) {
System.out.println(emp);
}
};
}
}
Total
project structure looks like below.
Run
App.java, you can see below messages in console.
************************************** Employee [id=5d53855a98c4b253c1a66745, firstName=Phalgun, lastName=Garimella] Employee [id=5d53855a98c4b253c1a66746, firstName=Sankalp, lastName=Dubey] Employee [id=5d53855a98c4b253c1a66747, firstName=Arpan, lastName=Garimella] Employee [id=5d53855a98c4b253c1a66748, firstName=Phalgun, lastName=Dubey] ************************************** Employee [id=5d53855a98c4b253c1a66745, firstName=Ram, lastName=Gurram] Employee [id=5d53855a98c4b253c1a66746, firstName=Sankalp, lastName=Dubey] Employee [id=5d53855a98c4b253c1a66747, firstName=Arpan, lastName=Garimella] Employee [id=5d53855a98c4b253c1a66748, firstName=Ram, lastName=Gurram]
You can
download complete working application from this link.
No comments:
Post a Comment