1、springdata 整合 mongodb 一些方法包括 or,and , regex 等等这几天接触 mongodb 以及 springdata ,自己英语比较戳,所以整理这些方法花的时间多了点,不过也是我第一次在外国网站整理技术不多说,直接上代码,这里只是给出一些操作方法而已,如果有需要源码的,请 Q 我206314068, 如转载请注明出处1 package mongodbProject1; 23 import java.util.List; 45 import mg.pojo.User; 6 import mg.pojo.UserList; 7 import mg.service.Us
2、erService; 8910 import org.springframework.context.ApplicationContext; 11 importorg.springframework.context.support.ClassPathXmlApplicationContext; 12 import org.springframework.data.mongodb.core.MongoTemplate; 13 import org.springframework.data.mongodb.core.query.Criteria; 14import org.springframew
3、ork.data.mongodb.core.query.CriteriaDefinition; 15 import org.springframework.data.mongodb.core.query.Query; 1617 import com.mongodb.CommandResult; 18 import com.mongodb.DBObject; 1920 publicclass Test 21 static ApplicationContext context= null ; 22 static MongoTemplatemongoTemplate=null ; 23 static
4、 24context= newClassPathXmlApplicationContext(“applicationContext.xml“); 25 mongoTemplate=context.getBean(MongoTemplate. class ); 26 27 /*28 * 查询 UserName中等于 123 的29 * where(String n) is(String s) 30*/31 org.junit.Test 32 publicvoid TestFind() 3334 Query query=Query.query( 35 Criteria.where(“UserNam
5、e“).is(“123“); / is 相当于 sql 语句中的 =36 DBObjectobj=query.getFieldsObject(); 37 try 38 Listuserlist=mongoTemplate.find(query, User. class ); 39 System.out.println(userlist); 40 catch (Exception e)e.printStackTrace(); 4142 43 /*44 * all() 方法是相当于 and 一样,功能是查询所有某个类型是数组或列表的字段中包含有 “00“ 与 “lzh“ 的记录具体详见45 *ht
6、tp:/docs.mongodb.org/manual/reference/operator/query/all/46 * 测试数据:47 * 4849 Password: “xyz“, 50 UserName: “school“, “book“, “bag“, “headphone“, “appliance“ , 51 52*/53 org.junit.Test 54 publicvoid testAll() 55 Query query=Query.query(Criteria.where(“UserName“).all(“00“,“lzh“); 56 try 57 Listuserlis
7、t=mongoTemplate.find(query, UserList. class );System.out.println(userlist); 58 catch (Exception e)e.printStackTrace(); 59 60 /*61 * elemMatch() 方法使用,其数据库格式如下62 * 查询的是对象数组下对象属性是否匹配相应的值63 * 数据格式如下:64 *db.inventory.find( 65 qty: $all: 66 “$elemMatch“ : size: “M“, num: $gt: 50 , 67 “$elemMatch“ : num :
8、100, color: “green“ 68 69 ) 70*/71 org.junit.Test 72 publicvoid testelemMatch() 73 Criteria c= newCriteria(); 74 Query qm= newQuery(); 75qm.addCriteria(c.elemMatch(Criteria.where(“UserName“).is(“lzh1“).and(“Password“).is(100); / 括号里的字符串是数据字段名称76 DBObject s=qm.getQueryObject(); / 转换成 DBObject 为了更方便获取
9、得到字符串命令77 String n=s.toString(); 78 Query query=Query.query(Criteria.where(“user“).all(s); 79 try 80 Listuserlist=mongoTemplate.find(query, UserList. class );System.out.println(“list 大小“+userlist.size()+“n“+userlist); 81 catch (Exception e)e.printStackTrace(); 82 83 /*84 * and 操作,相当于 sql 语句中的 and 85
10、*/86 org.junit.Test 87 publicvoid testAnd() 88 Query query=Query.query(Criteria.where(“UserName“).is(“00“).and(“Password“).is(“123“); 89 try 9091 Listuserlist=mongoTemplate.find(query, User. class );System.out.println(“list 大小“+userlist.size()+“n“+userlist); 92 catch (Exception e)e.printStackTrace()
11、; 93 94 /*95 * 该方法是使用 regex() (正则表达式)方法以及 or (或)操作查询数据96 * 相当于 db.user.find( “UserName“ : “00“, “$or“ : “Password“ : /lz/ ); 97*/98 org.junit.Test 99 publicvoid testor() 100101try 102 Criteria c=Criteria.where(“Password“).regex(“lz“); / 这里的正则表达式是 /lzh/103 Query query=Query.query(Criteria.where(“User
12、Name“).is(“00“).orOperator(c); 104105 Listuserlist=mongoTemplate.find(query, User. class ); 106System.out.println(“list 大小 “+userlist.size()+“n“+userlist); 107 catch (Exception e)e.printStackTrace(); 108 109/* 使用正则表达式查询110 * Criteria.where(“Password“).regex(re, options); 其中 re,option 都是字符串 , 111 * o
13、ption 可以选值为: i,m,x,si 表示不区分大小写, m表示能使用 以及 $等正则表达式来识别数据库中使用 n 换行的每一行开始字符以及字符。112 * x 113 * 具体原文介绍http:/docs.mongodb.org/manual/reference/operator/query/regex/ 114*/115 org.junit.Test 116publicvoid testRegex() 117118try 119 Criteria c=Criteria.where(“Password“).regex(“lz“,“i“); /这里的正则表达式是 /lzh/120121 Query query=Query.query(c); 122123 Listuserlist=mongoTemplate.find(query, User. class ); 124System.out.println(“list 大小 “+userlist.size()+“n“+userlist); 125 catch (Exception e)e.printStackTrace(); 126 127