mongoTemplate通过Query条件指定查询条件和返回字段
本文转自Spring Data MongoDB 基本文档查询(Query、BasicQuery),仅做学习备份,如有侵权请联系博主删除!一.简介Spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我们介绍了对MongoDB的新增和删除, 今天我们要介绍Java代码实
本文转自 Spring Data MongoDB 基本文档查询(Query、BasicQuery) ,仅做学习备份,如有侵权请联系博主删除!
一.简介
Spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我们介绍了对MongoDB的新增和删除, 今天我们要介绍Java代码实现对MongoDB实现查询操作。
我们回顾一下,我们在之前介绍了MongoDB的基本文档查询,MongoDB的查询语法:
db.orders.find({{<field1>:<value1>,<field2>: <value2>, ... } },{field1:<boolean>, field2: <boolean> ... })
我们介绍是SpringData MongoDB 提供了find方法,方便我们通过java代码实现对MongoDB的查询操作:
mongoTemplate.find (query, entityClass)
参数说明:
entityClass:实体class,也就是要把文档转换成对应的实体。
query查询语句的实现的方式有两种:
1.org.springframework.data.mongodb.core.query
构造函数
Query (Criteria criteria)
接受的参数是org.springframework.data.mongodb.core.query.Criteria
Criteria是标准查询的接口,可以引用静态的Criteria.where的把多个条件组合在一起,就可以轻松地将多个方法标准和查询连接起来,方便我们操作查询语句。
例如: 查询条件onumber="002"
mongoTemplate.find (new Query(Criteria.where("onumber").is("002")),entityClass)
多个条件组合查询时:
例如:onumber="002" and cname="zcy"
mongoTemplate.find (new Query(Criteria.where("onumber").is("002").and("cname").is("zcy")),entityClass)
例如:onumber="002" or cname="zcy"
mongoTemplate.findOne(newQuery(newCriteria().orOperator(Criteria.where("onumber").is("002"),Criteria.where("cname").is("zcy"))),entityClass);
我们通过Criteria的and方法,把这个条件组合一起查询
Criteria提供了很多方法,我们这边先介绍基本文档的查询操作符,对于数组文档或者内嵌文档的操作符,我们下一篇在介绍。
Criteria | Mongodb | 说明 |
Criteria and (String key) | $and | 并且 |
Criteria andOperator (Criteria… criteria) | $and | 并且 |
Criteria orOperator (Criteria… criteria) | $or | 或者 |
Criteria gt (Object o) | $gt | 大于 |
Criteria gte (Object o) | $gte | 大于等于 |
Criteria in (Object… o) | $in | 包含 |
Criteria is (Object o) | $is | 等于 |
Criteria lt (Object o) | $lt | 小于 |
Criteria lte (Object o) | $lte | 小等于 |
Criteria nin (Object… o) | $nin | 不包含 |
。。。。。。。。。。。。。。 |
2、子类 org.springframework.data.mongodb.core.query.BasicQuery
构造方法
BasicQuery(DBObject queryObject)
BasicQuery(DBObject queryObject, DBObject fieldsObject)
BasicQuery(java.lang.String query)
BasicQuery(java.lang.String query, java.lang.String fields)
DBObject就是转换成JSON格式,提供了我们回顾一下,MongoDB查询时,
db.collection.find(query,projection),query类型是document,所以,我们想使用JSON字符串查询时,我们使用DBObject创建查询实例。
DBObject是接口,提供了几个子类,
我们比较经常使用的比较底层子类,扩展了自己的方法和继承父类,所以功能会比较多。
1. BasicDBObject
BasicBSONObject extendsLinkedHashMap<String,Object> implements BSONObject
BasicDBObject extends BasicBSONObject implementsDBObject
例如:查询条件onumber="002"
DBObject obj = new BasicDBObject();
obj.put( "onumber","002" );
相当于
db.collect.find({"onumber":"002"})
2. BasicDBList
BasicBSONList extendsArrayList<Object> implements BSONObject
BasicDBList extends BasicBSONList implements DBObject
BasicDBList可以存放多个BasicDBObject条件
例如:我们查询onumber=002OR cname=zcy1
BasicDBList basicDBList=new BasicDBList();
basicDBList.add(new BasicDBObject("onumber","002"));
basicDBList.add(new BasicDBObject("cname","zcy1"));
DBObjectobj =newBasicDBObject();
obj.put("$or", basicDBList);
Query query=new BasicQuery(obj);
相当于
db.orders.find({$or:[{"onumber":"002"},{"cname":"zcy1"}]})
basicDBList.add方法是添加一个文档的查询条件
3. com.mongodb. QueryBuilder
QueryBuilder默认构造函数,是初始化BasicDBObject,QueryBuilder多个方法标准和查询连接起来,方便我们操作查询语句。跟Criteria是标准查询的接口一样,
QueryBuilder和BasicDBObject配合使用
QueryBuilder帮我们实现了 $and等操作符,我们查看部分的源代码:QueryBuilder部分的源代码:
[java] view plain copy
- publicclassQueryBuilder {
- /**
- * Creates a builder with an empty query
- */
- publicQueryBuilder() {
- _query = new BasicDBObject();
- }
- publicQueryBuilder or( DBObject ... ors ){
- List l = (List)_query.get( "$or" );
- if ( l == null ){
- l = new ArrayList();
- _query.put( "$or" , l );
- }
- for ( DBObject o : ors )
- l.add( o );
- return this;
- }
- /**
- * Equivalent to an $and operand
- * @param ands
- * @return
- */
- @SuppressWarnings("unchecked")
- publicQueryBuilder and( DBObject ... ands ){
- List l = (List)_query.get( "$and" );
- if ( l == null ){
- l = new ArrayList();
- _query.put( "$and" , l );
- }
- for ( DBObject o : ands )
- l.add( o );
- return this;
- }
接下来我们介绍查询的实现, Criteria提供了很多方法,我们这边就不在一个一个的操作符执行一遍,这跟学习MongoDB 四: MongoDB查询(一)基本文档的操作符介绍的一样。
二.findOne查询
findOne返回满足指定查询条件的文档,如果多个文档满足查询,该方法返回第一个文档,根据自然顺序返回文件在磁盘上的顺序,在覆盖的集合中,自然顺序与插入顺序相同。如果没找到对应的文档,会返回null。
方法:
mongoTemplate.findOne(query,entityClass)
1. 介绍接口以及方法的实现
我们在上一篇有介绍了实现基本的添加,对整个结构有介绍了,我们这边就不在介绍了,直接介绍往里面添加方法
第一步:我们在基础接口MongoBase.java类新增一个findOne的接口
[java] view plain copy
- //根据条件查询
- blic T findOne(Query query,String collectionName);
第二步:我们在OrdersDaoImpl类添加一个具体findOne的实现方法
[java] view plain copy
- @Override
- ublic Orders findOne(Query query, String collectionName) {
- return mongoTemplate.findOne(query, Orders.class, collectionName);
第三步:实现测试方法
[java] view plain copy
- /测试testFindOne方法添加
- @Test
- public void testFindOne() throws ParseException
- {
- Queryquery=newQuery(Criteria.where("onumber").is("002"));
- Ordersorder=ordersDao.findOne(query,collectionName);
- System.out.println(JSONObject.fromObject(order));
- }
我们到MongoDB查询时,有两条onumber值相同的文档
[sql] view plain copy
- > db.orders.find()
- { "_id" : ObjectId("55b3ae9bee10ded9390d0b97"),"_class" : "com.mongo.model.Orders", "onumber" : "002", "date" :ISODate("2015-01-24T16:07:00Z"), "cname" : "zcy1", "items" : [ { "quantity" : 5,"price" : 4, "pnumber" : "p001" }, {"quantity" : 6, "price" : 8, "pnumber" :"p002" } ] }
- { "_id" : ObjectId("55b3aea5ee10f970a2da7017"),"_class" : "com.mongo.model.Orders", "onumber" : "002", "date" :ISODate("2015-01-24T16:07:00Z"), "cname" : "zcy2", "items" : [ { "quantity" : 5,"price" : 4, "pnumber" : "p003" }, { "quantity" : 6, "price" : 8, "pnumber" :"p004" } ] }
我们执行findOne时查询条件为onumber=002,返回第一个记录
[sql] view plain copy
- {"cname":"zcy1","date"{"date":25,"day":0,"hours":0,"minutes":7,"month":0,"seconds":0,"time":1422115620000,"timezoneOffset":-480,"year":115},"id":"55b3ae9bee10ded9390d0b97","items":[{"pnumber":"p001","price":4,"quantity":5},{"pnumber":"p002","price":8,"quantity":6}],"onumber":"002"}
三.find查询
1.org.springframework.data.mongodb.core.query
构造函数
Query (Criteria criteria)
接受的参数是org.springframework.data.mongodb.core.query.Criteria
例子:查询onumber="002" 并且cname="zcy"
OrdersDaoImpl类实现了find的方法
[java] view plain copy
- @Override
- publicList<Orders> find(org.springframework.data.mongodb.core.query.Queryquery, String collectionName) {
- return mongoTemplate.find(query, Orders.class, collectionName);
- }
实现测试方法
Query query=new Query(Criteria.where("onumber").is("002").and("cname").is("zcy1"));
[java] view plain copy
- @Test
- public void testFind() throws ParseException
- {
- Queryquery=newQuery(Criteria.where("onumber").is("002").and("cname").is("zcy1"));
- List<Orders>orders=ordersDao.find(query,collectionName);
- System.out.println(JSONArray.fromObject(orders));
- }
我们查看转换成Query 时,是怎么样的,我们断点跟踪一下
会转换成对应的文档查询
查询的结果
[java] view plain copy
- [{"cname":"zcy1","date":{"date":25,"day":0,"hours":0,"minutes":7,"month":0,"seconds":0,"time":1422115620000,"timezoneOffset":-480,"year":115},"id":"55b3ae9bee10ded9390d0b97","items":[{"pnumber":"p001","price":4,"quantity":5},{"pnumber":"p002","price":8,"quantity":6}],"onumber":"002"}]
相当于MongoDB
b.orders.find({"onumber" : "002" ,"cname" : "zcy1"})
还可以另外一种写法Criteria andOperator(Criteria… criteria)
Queryquery=newQuery(Criteria.where("onumber").is("002").andOperator(Criteria.where("cname").is("zcy1")));
一个Criteria中只能有一个andOperator,and可以多个,我们查询并列条件时,比较建议使用and方法。
2、org.springframework.data.mongodb.core.query.BasicQuery
构造方法
BasicQuery(DBObject queryObject)
BasicQuery(DBObject queryObject, DBObject fieldsObject)
BasicQuery(java.lang.String query)
BasicQuery(java.lang.String query, java.lang.String fields)
例子:查询onumber="002" or cname="zcy"
OrdersDaoImpl类实现了find的方法
[java] view plain copy
- @Override
- publicList<Orders> find(org.springframework.data.mongodb.core.query.BasicQueryquery, String collectionName) {
- returnmongoTemplate.find(query, Orders.class, collectionName);
- }
实现测试方法
[java] view plain copy
- public voidtestFind() throwsParseException
- {
- BasicDBListbasicDBList=newBasicDBList();
- basicDBList.add(new BasicDBObject("onumber","002"));
- basicDBList.add(new BasicDBObject("cname","zcy1"));
- DBObjectobj = newBasicDBObject();
- obj.put("$or", basicDBList);
- Queryquery=newBasicQuery(obj);
- List<Orders>orders=ordersDao.find(query,collectionName);
- System.out.println(JSONArray.fromObject(orders));
- }
查询的结果:
[sql] view plain copy
- [{"cname":"zcy1","date":{"date":25,"day":0,"hours":0,"minutes":7,"month":0,"seconds":0,"time":1422115620000,"timezoneOffset":-480,"year":115},"id":"55bb9a3c27547f55fef9a10f","items":[{"pnumber":"p001","price":5,"quantity":6},{"pnumber":"p002","price":9,"quantity":7}],"onumber":"001"},
[sql] view plain copy
- {"cname":"zcy1","date":{"date":25,"day":0,"hours":0,"minutes":7,"month":0,"seconds":0,"time":1422115620000,"timezoneOffset":-480,"year":115},"id":"55bb9a2727544d40b95156e1","items":[{"pnumber":"p001","price":5,"quantity":6},{"pnumber":"p002","price":9,"quantity":7}],"onumber":"001"}]
相当于MongoDB
{ "$or" : [ { "onumber" :"002"} , { "cname" : "zcy1"}]}
QueryBuilder和BasicDBObject配合使用
QueryBuilder queryBuilder= newQueryBuilder();
queryBuilder.or(new BasicDBObject("onumber","002"),newBasicDBObject("cname","zcy1"));
Query query=new BasicQuery(queryBuilder.get());
四.find查询时指定返回的需要的字段
org.springframework.data.mongodb.core.query.BasicQuery提供了
构造方法
BasicQuery(DBObject queryObject, DBObject fieldsObject)
BasicQuery(java.lang.String query, java.lang.String fields)
BasicQuery查询语句可以指定返回字段,构造函数
BasicQuery(DBObject queryObject, DBObject fieldsObject)
fieldsObject 这个字段可以指定返回字段
fieldsObject.put(key,value)
key:字段
value:
说明:
1或者true表示返回字段
0或者false表示不返回该字段
_id:默认就是1,没指定返回该字段时,默认会返回,除非设置为0是,就不会返回该字段。
指定返回字段,有时文档字段多并数据大时,我们指定返回我们需要的字段,这样既节省传输数据量,减少了内存消耗,提高了性能,在数据大时,性能很明显的。
[java] view plain copy
- QueryBuilder queryBuilder = new QueryBuilder();
- queryBuilder.or(new BasicDBObject("onumber", "002"), new BasicDBObject("cname","zcy1"));
- BasicDBObject fieldsObject=new BasicDBObject();
- fieldsObject.put("onumber", 1);
- fieldsObject.put("cname", 1);
- uery query=new BasicQuery(queryBuilder.get(),fieldsObject);
返回结果:
[java] view plain copy
- [{"cname":"zcy1","date":null,"id":"55bb9a3c27547f55fef9a10f","items":[],"onumber":"001"},{"cname":"zcy1","date":null,"id":"55bb9a2727544d40b95156e1","items":[],"onumber":"001"}]
相当于MongoDB
db.orders.find({"$or" : [ { "onumber" : "002"} , {"cname" : "zcy1"}]},{"onumber":1,"cname":1})
更多推荐
所有评论(0)