Class类
@Document(collection = "class")
public class Class {
private String classId;
private String teacher;
private List<Student> Students;
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getTeacher(){
return teacher;
}
public void setTeacher(String teacher){
this.teacher = teacher;
}
public List<Student> getStudents() {
return Students;
}
public void setStudents(List<Student> Students) {
this.Students = Students;
}
@Override
public String toString() {
return "Class [classId=" + classId + ", Students=" + Students + "]";
}
}
查询满足条件的数据
Query query = Query.query(Criteria.where("classId").is("1"));
List<Class> classs = mongoTemplate.find(query,Class.class,"class");
System.out.println(classs);
查询满足条件的第一条数据
Query query = Query.query(Criteria.where("classId").is("1"));
List<Class> classs = mongoTemplate.findOne(query,Class.class,"class");
System.out.println(classs);
筛选要查询的字段(id字段不查出来)
DBObject dbObject = new BasicDBObject();
dbObject.put("classId", "1");
DBObject fieldObject = new BasicDBObject();
fieldObject.put("_id", false);
Query query = new BasicQuery(dbObject, fieldObject);
List<HashMap> classs = mongoTemplate.find(query,HashMap.class,"class");
System.out.println(classs);
当然也可以不使用HashMap,而使用自己封装的dto,那么只会查询出dto中的字段。
查询结果排序
DBObject dbObject = new BasicDBObject();
dbObject.put("classId", "1");
DBObject fieldObject = new BasicDBObject();
fieldObject.put("_id", false);
Query query = new BasicQuery(dbObject);
query.with(new Sort(Direction.ASC,"classId"));
List<HashMap> classs = mongoTemplate.find(query,HashMap.class,"class");
System.out.println(classs);
多字段排序
DBObject dbObject = new BasicDBObject();
dbObject.put("classId", "1");
DBObject fieldObject = new BasicDBObject();
fieldObject.put("_id", false);
Query query = new BasicQuery(dbObject);
List<Sort.Order> orders = new ArrayList<>();
orders.add(new Sort.Order(Direction.DESC, "classId"));
orders.add(new Sort.Order(Direction.DESC, "teacher"));
query.with(new Sort(orders));
List<HashMap> classs = mongoTemplate.find(query,HashMap.class,"class");
System.out.println(classs);
查询结果分页
int pageNum = 1;
int pageSize = 10;
DBObject dbObject = new BasicDBObject();
dbObject.put("classId", "1");
DBObject fieldObject = new BasicDBObject();
fieldObject.put("_id", false);
Query query = new BasicQuery(dbObject,fieldObject);
List<Sort.Order> orders = new ArrayList<>();
orders.add(new Sort.Order(Direction.ASC, "classId"));
orders.add(new Sort.Order(Direction.DESC, "teacher"));
query.with(new Sort(orders));
query.skip(pageSize*(pageNum-1)).limit(pageSize);
List<HashMap> classs = mongoTemplate.find(query,HashMap.class,"class");
System.out.println(classs);
还有一种通过Morphia框架的做法:
int pageNum = 1;
int pageSize = 10;
DBObject dbObject = new BasicDBObject();
dbObject.put("teacher", "Mr.wang");
DBObject fieldObject = new BasicDBObject();
fieldObject.put("_id", false);
fieldObject.put("_class", false);
DBObject sortDBObject=new BasicDBObject();
sortDBObject.put("classId",1);
DBCursor result = mongoTemplate.getCollection("class")
.find(dbObject, fieldObject)
.sort(sortDBObject)
.skip(pageSize*(pageNum-1)).limit(pageSize);
System.out.println(result.count());
System.out.println(result.toArray());
多条件查询
我们只需要对query对象做出处理即可
Criteria criteria = new Criteria(); criteria.orOperator(Criteria.where("classId").is("2"),Criteria.where("teacher").is("Mr.wang"));
criteria.andOperator(Criteria.where("classId").is("2"));
query.addCriteria(criteria);