这几天接触mongodb以及springdata,自己英语比较戳,所以整理这些方法花的时间多了点,不过也是我第一次在外国网站整理技术

不多说,直接上代码,这里只是给出一些操作方法而已,如果有需要源码的,请Q我206314068,如转载请注明出处

 package mongodbProject1;

 import java.util.List;

 import mg.pojo.User;
import mg.pojo.UserList;
import mg.service.UserService; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query; import com.mongodb.CommandResult;
import com.mongodb.DBObject; public class Test {
static ApplicationContext context=null;
static MongoTemplate mongoTemplate=null;
static{
context=new ClassPathXmlApplicationContext("applicationContext.xml");
mongoTemplate=context.getBean(MongoTemplate.class);
}
/**
* 查询UserName中等于123的
* where(String n) is(String s)
*/
@org.junit.Test
public void TestFind(){ Query query=Query.query(
Criteria.where("UserName").is("123"));// is相当于sql语句中的=
DBObject obj=query.getFieldsObject();
try{
List<User>userlist=mongoTemplate.find(query, User.class);
System.out.println(userlist);
}catch(Exception e){e.printStackTrace();} }
/**
* all()方法是相当于and一样,功能是查询所有某个类型是数组或列表的字段中包含有"00"与"lzh"的记录具体详见
* http://docs.mongodb.org/manual/reference/operator/query/all/
* 测试数据:
* { Password: "xyz",
UserName: [ "school", "book", "bag", "headphone", "appliance" ],
}
*/
@org.junit.Test
public void testAll(){
Query query=Query.query(Criteria.where("UserName").all("00","lzh"));
try{
List<UserList>userlist=mongoTemplate.find(query, UserList.class);System.out.println(userlist);
}catch(Exception e){e.printStackTrace();}
}
/**
* elemMatch()方法使用,其数据库格式如下
* 查询的是对象数组下对象属性是否匹配相应的值
* 数据格式如下:
* db.inventory.find( {
qty: { $all: [
{ "$elemMatch" : { size: "M", num: { $gt: 50} } },
{ "$elemMatch" : { num : 100, color: "green" } }
] }
} )
*/
@org.junit.Test
public void testelemMatch(){
Criteria c=new Criteria();
Query qm=new Query();
qm.addCriteria(c.elemMatch(Criteria.where("UserName").is("lzh1").and("Password").is(100)));//括号里的字符串是数据字段名称
DBObject s=qm.getQueryObject();//转换成DBObject为了更方便获取得到字符串命令
String n=s.toString();
Query query=Query.query(Criteria.where("user").all(s));
try{
List<UserList>userlist=mongoTemplate.find(query, UserList.class);System.out.println("list大小"+userlist.size()+"\n"+userlist);
}catch(Exception e){e.printStackTrace();}
}
/**
* and操作,相当于sql语句中的and
*/
@org.junit.Test
public void testAnd(){
Query query=Query.query(Criteria.where("UserName").is("00").and("Password").is("123"));
try{ List<User>userlist=mongoTemplate.find(query, User.class);System.out.println("list大小"+userlist.size()+"\n"+userlist);
}catch(Exception e){e.printStackTrace();}
}
/**
* 该方法是使用regex()(正则表达式)方法以及or(或)操作查询数据
* 相当于db.user.find({ "UserName" : "00", "$or" : [{ "Password" : /lz/ }] });
*/
@org.junit.Test
public void testor(){ try{
Criteria c=Criteria.where("Password").regex("lz");//这里的正则表达式是/lzh/
Query query=Query.query(Criteria.where("UserName").is("00").orOperator(c)); List<User>userlist=mongoTemplate.find(query, User.class);
System.out.println("list大小"+userlist.size()+"\n"+userlist);
}catch(Exception e){e.printStackTrace();}
}
/**使用正则表达式查询
* Criteria.where("Password").regex(re, options);其中re,option 都是字符串,
* option可以选值为:i,m,x,s i表示不区分大小写,m表示能使用^以及$等正则表达式来识别数据库中使用\n换行的每一行开始字符以及字符。
* x
* 具体原文介绍http://docs.mongodb.org/manual/reference/operator/query/regex/
*/
@org.junit.Test
public void testRegex(){ try{
Criteria c=Criteria.where("Password").regex("lz","i");//这里的正则表达式是/lzh/ Query query=Query.query(c); List<User>userlist=mongoTemplate.find(query, User.class);
System.out.println("list大小"+userlist.size()+"\n"+userlist);
}catch(Exception e){e.printStackTrace();}
}
}

springdata整合mongodb一些方法包括or,and,regex等等《有待更新》的更多相关文章

  1. java操作mongodb & springboot整合mongodb

    简单的研究原生API操作MongoDB以及封装的工具类操作,最后也会研究整合spring之后作为dao层的完整的操作. 1.原生的API操作 pom.xml <!-- https://mvnre ...

  2. SpringBoot+SpringData 整合入门

    SpringData概述 SpringData :Spring的一个子项目.用于简化数据库访问,支持NoSQL和关系数据存储.其主要目标是使用数据库的访问变得方便快捷. SpringData 项目所支 ...

  3. Spring整合MongoDB(转)

    1.认识Spring Data MongoDB 之前还的确不知道Spring连集成Nosql的东西都实现了,还以为自己又要手动封装一个操作MongoDB的API呢,结果就发现了Spring Data ...

  4. Springboot 整合 MongoDB

    Springboot 整合 MongoDB 这节我们将整合 Spring Boot 与 Mongo DB 实现增删改查的功能,并且实现序列递增. Mongo DB 的基本介绍和增删改查的用法可以参考我 ...

  5. SpringBoot 整合 MongoDB 实战介绍

    一.介绍 在前面的文章中,我们详细的介绍了 MongoDB 的配置和使用,如果你对 MongoDB 还不是很了解,也没关系,在 MongoDB 中有三个比较重要的名词:数据库.集合.文档! 数据库(D ...

  6. SpringBoot整合mongoDB

    MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. 这一片文章介绍一个springboot整合mongodb,如果你了解整合mysql之类的 ...

  7. springboot 学习之路 14(整合mongodb的Api操作)

    springboot整合mongodb: mongodb的安装和权限配置  请点击连接参考 mongodb集成 : 第一步:引如pom文件 第二步:配置文件配置mongodb路径: 第三步:关于mon ...

  8. SpringMVC整合Mongodb开发,高级操作

    开发环境: 操作系统:windows xpMongodb:2.0.6依 赖 包:Spring3.2.2 + spring-data-mongodb-1.3.0 + Spring-data-1.5 +  ...

  9. spring整合mongodb

    使用spring整合mongodb maven 依赖 <dependency> <groupId>org.mongodb</groupId> <artifac ...

随机推荐

  1. UIView上添加了一个按钮和一个单击手势的事件相应,互相不影响的处理方法。。

    tapGesture.delegate = self; - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shoul ...

  2. [Practical Git] Show who changed a line last with git blame

    When working on a file, we often want to know who made certain changes last; we can use git blame to ...

  3. SQL书写技巧

    SQL书写技巧: 1.针对分区表,如果可以使用分区条件的,一定要加分区条件.分区条件的使用,可以减少不必要的数据访问,加快查询数据,如TB_CSV_ACCEPT_FLOW_OPERATOR表,以acc ...

  4. 【C语言】字符串替换空格:实现一个函数,把字符串里的空格替换成“%20”

    //字符串替换空格:实现一个函数,把字符串里的空格替换成"%20" #include <stdio.h> #include <assert.h> void ...

  5. 深刻理解Python中的元类(metaclass)

    译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得 ...

  6. 乐视mysql面试题

      http://blog.itpub.net/28916011/viewspace-2093197/ 最近,朋友去乐视面试了mysql DBA,以下是我据整理的乐视mysql面试题答案,供大家参考 ...

  7. [001]const和指针

    很经典的: const int* p: int* const p: 前者表示指针指向的值是const,指向的值不可变,但是指针本身是可变的:后者表示改指针是const,指针不可变,但是指向的值是可变的 ...

  8. 【转】cocos2d-x 2.0版本 自适应屏幕分辨率

    http://codingnow.cn/cocos2d-x/975.html 我使用的版本是cocos2d-2.0-x-2.0.4,cocos2dx-2.0版本对多分辨率适配提供了很好的支持,使用起来 ...

  9. Cookie API

    Cookie API All cookies created by the Nova framework are encrypted and signed with an authentication ...

  10. 16% off MPPS V16 ECU tuning tool for EDC15 EDC16 EDC17

    EOBD2.FR is offering 16% discount off the latest MPPS V16 ECU chip tuning tool. The device is now so ...