Spring Data MongoDB : Query document
Published: May 19, 2011 , Updated: May 18, 2011 , Author: mkyong
In Spring data for MongoDB, you can use findOne(), find() and getCollection() to get / query documents from mongoDB. See some of the common ways :
User user = new User("..."); //get first found record, from "test" collection, where id = "1001" User user = mongoOperation.findOne("test", new Query(Criteria .where("id").is("1001")), User.class); //get all found records, from "test" collection, where id <= "1000" abs age = 21 List<User> users = mongoOperation.find("test", new Query(Criteria .where("id").lte("2001").and("age").is(21)), User.class); //get all records from "test" collection List<User> users = mongoOperation.getCollection("test", User.class);
You can use “Query” and “Criteria” classes to define the query criteria easily.
MUST READ
You must read this Spring’s mongodb query documentation, to study more overloaded methods and criteria examples.
You must read this Spring’s mongodb query documentation, to study more overloaded methods and criteria examples.
Query documents example
Full example to show the use of “Spring Data MongoDB” APIs to query documents from 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 java.util.ArrayList; import java.util.List; 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 com.mkyong.config.SpringMongoConfig; import com.mkyong.user.User; public class QueryDocumentApp { public static void main(String[] args) { // For Annotation ApplicationContext ctx = new AnnotationConfigApplicationContext( SpringMongoConfig.class); MongoOperations mongoOperation = (MongoOperations) ctx .getBean("mongoTemplate"); User user1 = new User("2000", "user2000", "mkyong", 20); User user2 = new User("2001", "user2001", "mkyong", 21); User user3 = new User("2002", "user2002", "mkyong", 22); List<User> userList = new ArrayList<User>(); userList.add(user1); userList.add(user2); userList.add(user3); mongoOperation.insertList("test", userList); // Case 1 ... where id = 2000 System.out.println("Case 1..."); User userPrint1 = mongoOperation.findOne("test", new Query(Criteria .where("id").is("2000")), User.class); System.out.println(userPrint1); // Case 2 ... where id <= 2001 and age = 21 System.out.println("Case 2..."); List<User> users = mongoOperation.find("test", new Query(Criteria .where("id").lte("2001").and("age").is(21)), User.class); for (User temp : users) { System.out.println(temp); } // Case 3 ... where id <= 2001 and age = 21 System.out.println("Case 3..."); List<User> users2 = mongoOperation.getCollection("test", User.class); for (User temp : users2) { System.out.println(temp); } System.out.println("total number of users : " + users2.size()); } }
Output
Case 1... User [id=2000, firstname=user2000, lastname=mkyong, age=20] Case 2... User [id=2001, firstname=user2001, lastname=mkyong, age=21] Case 3... User [id=2000, firstname=user2000, lastname=mkyong, age=20] User [id=2001, firstname=user2001, lastname=mkyong, age=21] User [id=2002, firstname=user2002, lastname=mkyong, age=22] total number of users : 3
Download it – SpringData-MongoDB-Query-Document-Example.zip (7 KB)
References
Note : You can find more similar articles at - Java MongoDB Tutorials







Sorry, I did not understand, for sorting?
Thank you.
Hi,
I have just started up on spring-data-mongodb and am using version 1.0.0.M2 and mongo-java-driver 2.6.3. Your tutorial was very useful to start with spring-data. I am stuck on using sort() and findOne() together. My POJO to be inserted in mongodb is like this:
now i need to retrieve say tagName=”water temperature” that has been written in the mongodb latest. So i need to sort on timestamp. I am using the following approach:
but the sort is not working. How do i query for sort()? Am i using a wrong approach? In that case, Please correct me if possible. Thanks
[...] save(), updateFirst() and updateMulti() to update existing domain object from mongoDB database. Spring Data MongoDB : Query document Example to use Spring data findOne(), find() and getCollection() to get / query documents from [...]