import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
...
List<Employee> result =
mongoTemplate.find(query(where("age").lt(50).and("salary").gt(50000))
,Employee.class);
Criteria all (Object o)Creates a criterion using the $all operatorCriteria and (String key) Adds a chained Criteria with the specified key to the current Criteria and retuns the newly created oneCriteria andOperator (Criteria... criteria)Creates an and query using the $and operator for all of the provided criteria (requires MongoDB 2.0 or later)Criteria elemMatch (Criteria c) Creates a criterion using the $elemMatch operatorCriteria exists (boolean b) Creates a criterion using the $exists operatorCriteria gt (Object o)Creates a criterion using the $gt operatorCriteria gte (Object o)Creates a criterion using the $gte operatorCriteria in (Object... o) Creates a criterion using the $in operator for a varargs argument.Criteria in (Collection<?> collection) Creates a criterion using the $in operator using a collectionCriteria is (Object o)Creates a criterion using the $is operatorCriteria lt (Object o)Creates a criterion using the $lt operatorCriteria lte (Object o)Creates a criterion using the $lte operatorCriteria mod (Number value, Number remainder)Creates a criterion using the $mod operatorCriteria ne (Object o)Creates a criterion using the $ne operatorCriteria nin (Object... o) Creates a criterion using the $nin operatorCriteria norOperator (Criteria... criteria)Creates an nor query using the $nor operator for all of the provided criteriaCriteria not ()Creates a criterion using the $not meta operator which affects the clause directly followingCriteria orOperator (Criteria... criteria)Creates an or query using the $or operator for all of the provided criteriaCriteria regex (String re) Creates a criterion using a $regexCriteria size (int s)Creates a criterion using the $size operatorCriteria type (int t)Creates a criterion using the $type operatorQuery addCriteria (Criteria criteria) used to add additional criteria to the queryField fields () used to define fields to be included in the query resultsQuery limit (int limit) used to limit the size of the returned results to the provided limit (used for paging)Query skip (int skip) used to skip the provided number of documents in the results (used for paging)Sort sort () used to provide sort definition for the resultsEmployee employee = new Employee();
//get first found record, from "dojCollection" collection, where empId = 10001
Employee employee = mongoOperation.findOne(new Query(Criteria
.where("empId").is(10001)),"dojCollection", Employee.class);
//get all found records, from "dojCollection" collection, where empId <= 10001 abs empAge = 21
List<Employee> employees = mongoOperation.find(new Query(Criteria
.where("empId").lte(10001).and("empAge").is(21)), "dojCollection", Employee.class);
//get all records from "dojCollection" collection
List<Employee> employees = mongoOperation.getCollection("dojCollection", Employee.class);

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 + "]";
}
}
Step 2: Creating configuration file<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"/>
<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("dojCollection")) {
mongoOperations.dropCollection("dojCollection");
}
Employee employee3 = new Employee();
employee3.setEmpId(1001);
employee3.setEmpName("Dinesh Rajput");
employee3.setSalary(70000);
employee3.setEmpAge(26);
Employee employee4 = new Employee();
employee4.setEmpId(1002);
employee4.setEmpName("Adesh Rajput");
employee4.setSalary(30000);
employee4.setEmpAge(23);
Employee employee5 = new Employee();
employee5.setEmpId(1003);
employee5.setEmpName("Vinesh Rajput");
employee5.setSalary(32000);
employee5.setEmpAge(23);
Employee employee6 = new Employee();
employee6.setEmpId(1004);
employee6.setEmpName("Sweety Rajput");
employee6.setSalary(50000);
employee6.setEmpAge(22);
List<Employee> empList = new ArrayList<Employee>();
empList.add(employee3);
empList.add(employee4);
empList.add(employee5);
empList.add(employee6);
mongoOperations.insert(empList, "dojCollection");
System.out.println("***********************CASE 1************************");
// Case 1 ... where empId = 1001
// find
Employee employee1 = mongoOperations.findOne(query(where("empId").is(1001)), Employee.class,"dojCollection");
System.out.println(employee1);
System.out.println("***********************CASE 2************************");
// Case 2 ... where empAge >= 23 and salary = 70000
// find
List<Employee> employee2 = mongoOperations.find(query(where("salary").is(70000).and("empAge").gte(23)), Employee.class,"dojCollection");
System.out.println(employee2);
System.out.println("***********************CASE 3************************");
// Case 3 ... where empId <= 10002 and age = 23
// find
List<Employee> employee = mongoOperations.find(query(where("empId").lte(10002).and("empAge").is(23)), Employee.class,"dojCollection");
System.out.println(employee);
}
}
Step 4: Running the Examplepackage 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