MongDB之各種查詢操作
阿新 • • 發佈:2018-12-24
package com.net.test.mongdb.dao.imp; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query;import org.springframework.stereotype.Repository; import com.net.test.mongdb.dao.IMongDaoFind; import com.net.test.mongdb.entity.User; import com.net.test.mongdb.entity.UserP; import com.net.test.mongdb.entity.UserPsg; /** * @author *** * @Time:2017年8月4日 上午10:40:33 * @version 1.0 * Function: TODO*/ @Repository public class MongDaoFindImp implements IMongDaoFind{ @Resource protected MongoTemplate mongoTemplate; /** * @description 查詢所有使用者資訊 */ @Override public void findUsers() { List<User> list = mongoTemplate.findAll(User.class); for(User user : list) { System.out.println("name :" + user.getName() + " , age :" + user.getAge() + " , sex :" + user.getSex()); } } /** * @description 根據使用者名稱,單條查詢使用者資訊.如果有重複名稱,預設查詢第一條 * @param name */ public void findUserByName(String name) { Query query = Query.query(Criteria.where("name").is(name)); User user = mongoTemplate.findOne(query,User.class); System.out.println("name :" + user.getName() + " , age :" + user.getAge() + " , sex :" + user.getSex()); } /** * @description 模糊查詢 * @param name */ @Override public void findUserLikeName(String name) { Query query = new Query(); query.addCriteria(Criteria.where("name").regex(name)); List<User> list = mongoTemplate.find(query, User.class); for(User user : list) { System.out.println("name :" + user.getName() + " , age :" + user.getAge() + " , sex :" + user.getSex()); } } /** * @description 表名與pojo不相同進行對映 */ @Override public void findUserPs() { List<UserP> list = mongoTemplate.findAll(UserP.class, "user"); for(UserP user : list) { System.out.println("name :" + user.getName() + " , age :" + user.getAge() + " , sex :" + user.getSex() + " , hobby :" + user.getHobby()); } } /** * @description 測試多條件進行查詢,測試and連線符 */ @Override public void findUserAndOperator() { Query query = new Query(); Criteria c = new Criteria(); query.addCriteria(c.andOperator(Criteria.where("name").regex("車"), Criteria.where("age").is(18))); List<User> list = mongoTemplate.find(query, User.class); for(User user : list) { //基礎資訊 String name = user.getName(); int age = user.getAge(); String sex = user.getSex(); //內建物件 UserPsg psg = user.getPsg(); //內建陣列 ArrayList<String> listHobby = (ArrayList<String>) user.getHobby(); StringBuffer bf = new StringBuffer(); if(null != listHobby && listHobby.size() > 0) { for(String hobby : listHobby) { bf.append(hobby + ","); } }else { bf.append(""); } System.out.println( "name : " + name + ", age : " + age + ", sex : " + sex + ", hobby : " + bf.toString() + ", UserPsg : " + psg ); } } /** * @description 測試多條件進行查詢,測試平行並列or */ @Override public void findUserOrOperator() { Query query = new Query(); Criteria c = new Criteria(); query.addCriteria(c.orOperator(Criteria.where("name").regex("車"), Criteria.where("age").is(10))); List<User> list = mongoTemplate.find(query, User.class); for(User user : list) { //基礎資訊 String name = user.getName(); int age = user.getAge(); String sex = user.getSex(); //內建物件 UserPsg psg = user.getPsg(); //內建陣列 ArrayList<String> listHobby = (ArrayList<String>) user.getHobby(); StringBuffer bf = new StringBuffer(); if(null != listHobby && listHobby.size() > 0) { for(String hobby : listHobby) { bf.append(hobby + ","); } }else { bf.append(""); } System.out.println( "name : " + name + ", age : " + age + ", sex : " + sex + ", hobby : " + bf.toString() + ", UserPsg : " + psg ); } } /** * @description 測試多條件進行查詢,測試包含關係or連線符,此處的or和and有些相似 */ @Override public void findUserOrOperator2() { Query query = new Query(); query.addCriteria(Criteria.where("name").regex("車").orOperator(Criteria.where("age").is(19))); List<User> list = mongoTemplate.find(query, User.class); for(User user : list) { //基礎資訊 String name = user.getName(); int age = user.getAge(); String sex = user.getSex(); //內建物件 UserPsg psg = user.getPsg(); //內建陣列 ArrayList<String> listHobby = (ArrayList<String>) user.getHobby(); StringBuffer bf = new StringBuffer(); if(null != listHobby && listHobby.size() > 0) { for(String hobby : listHobby) { bf.append(hobby + ","); } }else { bf.append(""); } System.out.println( "name : " + name + ", age : " + age + ", sex : " + sex + ", hobby : " + bf.toString() + ", UserPsg : " + psg ); } } }