spring data mongodb之mongodbTemplate查询总结

常用的列表查询(筛选条件,查询字段,排序及分页),相当于关系型数据库的:

select fields from tableName where coditions order by field skip limit;
DBObject cond = new BasicDBObject();
//等于条件
cond.put("sex","male");
//非等于条件
cond.put("age", new BasicDBObject(QueryOperators.GTE,20)
            .append(QueryOperators.LTE,30));
//添加or条件(和上面的条件还是and关系)
BasicDBList orList = new BasicDBList();
DBObject orCond1 = new BasicDBObject();
orCond1.put("name", "lisi");
DBObject orCond2 = new BasicDBObject();
orCond2.put("name", "zhaoliu");
orList.add(orCond1);
orList.add(orCond2);
cond.put(QueryOperators.OR, orList);
//限制查询返回的字段
DBObject feild = new BasicDBObject();
feild.put("name", 1);//查询name
feild.put("_id", 0);//_id不查询
Query query = new BasicQuery(cond,feild);
//单字段倒序
query.with(new Sort(Direction.DESC,"age"));
//多字段排序
/*List<Order> orders = new ArrayList<Sort.Order>();
orders.add(new Order(Direction.DESC,"age"));
orders.add(new Order(Direction.ASC,"name"));
query.with(new Sort(orders));*/
//分页
query.skip(0).limit(10);
List<HashMap> result = mongoTemplate.find(query, HashMap.class,"person");

QueryOperators常用的比较符:

OR = "$or"or条件
AND = "$and"and条件
GT = "$gt":大于操作
GTE = "$gte":大于等于操作
LT = "$lt":小于操作
LTE = "$lte"小于等于操作
NE = "$ne":不等于操作
IN = "$in"in操作

//示例
BasicDBList fieldList = new BasicDBList();
fieldList.add("上海");
cond.put("province", new BasicDBObject(QueryOperators.IN,fieldList));

NIN = "$nin"not in
MOD = "$mod";

//示例(age和6取模为0的数据)
BasicDBList modList = new BasicDBList();
modList.add(6);
modList.add(0);
cond.put("age", new BasicDBObject(QueryOperators.MOD,modList));

ALL = "$all":字段同时满足all中的所有条件,可以和$elemMatch配合使用
SIZE = "$size":数组的长度
EXISTS = "$exists":字段是否存在筛选(true,false)
ELEM_MATCH = "$elemMatch":内嵌文档完全匹配查询
WHERE = "$where"where条件,一般用于文档中两个字段的比较

//示例(name字段的值和province的值不等的数据)
cond.put(QueryOperators.WHERE,"this.name != this.province");

NOR = "$nor":同时不满足
TYPE = "$type":字段类型匹配
//模糊查询
cond.put("province", new BasicDBObject("$regex","^.*上.*$"));
NOT = "$not":不满足指定条件,或者该字段不存在
ORDER_BY = "$orderby":添加排序字段

这里列出的是比较常用的一些操作符,还有一些比较复杂的(例如计算点一点之间距离条件的)请参考:

使用游标的方式进行分页查询:

DBObject cond = new BasicDBObject();
//等于条件
cond.put("sex","male");
//非等于条件
cond.put("age", new BasicDBObject(QueryOperators.GTE,20)
            .append(QueryOperators.LTE,30));
//添加or条件(和上面的条件还是and关系)
BasicDBList orList = new BasicDBList();
DBObject orCond1 = new BasicDBObject();
orCond1.put("name", "lisi");
DBObject orCond2 = new BasicDBObject();
orCond2.put("name", "zhaoliu");
orList.add(orCond1);
orList.add(orCond2);
cond.put(QueryOperators.OR, orList);
//限制查询返回的字段
DBObject feild = new BasicDBObject();
feild.put("name", 1);//查询name
feild.put("_id", 0);//_id不查询
//排序字段
DBObject order = new BasicDBObject();
order.put("age",1);
DBCursor dbCursor = mongoTemplate.getCollection("person").find(cond, feild).sort(order).skip(0).limit(10);
System.err.println(dbCursor.count());
System.err.println(dbCursor.toArray());

根据某字段去重查询:

DBObject cond = new BasicDBObject();
cond.put("sex","male");
List distinct = mongoTemplate.getCollection("person").distinct("name",cond);

根据查询条件查询条数:

DBObject cond = new BasicDBObject();
cond.put("sex","male");
long count = mongoTemplate.getCollection("person").count(cond);
System.out.println(count);

使用Aggregation Pipeline进行聚群分析:

List<AggregationOperation> aggregationOptions = new ArrayList<AggregationOperation>();      aggregationOptions.add(Aggregation.match(Criteria.where("province").is("上海")));     aggregationOptions.add(Aggregation.group("age").count().as("count"));
aggregationOptions.add(Aggregation.project("count").and("age").previousOperation().andExclude("_id"));
aggregationOptions.add(Aggregation.sort(Direction.DESC,"age"));
aggregationOptions.add(Aggregation.skip(0L));
aggregationOptions.add(Aggregation.limit(10));
Aggregation agg = Aggregation.newAggregation(aggregationOptions);
AggregationResults<HashMap> aggregate = mongoTemplate.aggregate(agg,"person", HashMap.class);
List<HashMap> mappedResults = aggregate.getMappedResults();

其中group后可以添加一些聚合函数,如sum,avg等。如果遇到内嵌文档,需要拆分的情况,我们可以使用unwind将内嵌文档进行拆分:

aggregationOptions.add(Aggregation.unwind(field));
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值