【MongoDB】常用操作记录

【基础相关】

1、范围查询

db.getCollection("colName").find({"age":{$gte:10}})

2、排序

db.getCollection("colName").find().sort({"type":-1})

3、分组

db.getCollection("colName").aggregate({"$group":{"_id":"$type",'count':{"$sum":1}}})

4、查询字段值为空的数据

db.getCollection('colName').find({type:{$in:[null]}}) 或 db.getCollection('colName').find({type:null]})

5、查询字段值不为空的数据

db.getCollection("colName").find({type:{$ne:null}})

6、模糊查询

db.getCollection("colName").find({type:/A/})

db.getCollection("colName").find({type:/^A/})

7、AND条件基本语法
db.collection.find({key1:value1, key2:value2})

8、OR条件基本语法
db.collection.find({
 $or: [ {key1: value1},{key2:value2}]
})

9、范围查询
-- 查询users集合中年龄大于18岁的文档数据
db.users.find({age : {$gt : 18}})
-- 查询users集合中年龄小于18岁的文档数据
db.users.find({age : {$lt : 18}})
-- 查询users集合中年龄大于等于18岁的文档数据
db.users.find({age : {$gte : 18}})
-- 查询users集合中年龄大于等于18岁的文档数据
db.users.find({age : {$lte : 18}})

--查询某个时间段的数据

db.getCollection("test").find({"$and":[{"createTime":{$gte:ISODate("2021-01-27T00:00:00Z")}},{"createTime":{$lte:ISODate("2021-01-27T23:59:59Z")}}]}).count()

10、更新字段
db.test.updateMany({"create_time":{$exists: false}},{$set:{"create_time": "2022-08-22 11:08:32"}})
 

【Java相关】

1、模糊匹配

Criteria criteria = Criteria.where("字段名称").regex(".*?" + query + ".*?");

//完全匹配
Pattern pattern = Pattern.compile("^" + query + "$", Pattern.CASE_INSENSITIVE);
//右匹配
Pattern pattern = Pattern.compile("^.*" + query + "$", Pattern.CASE_INSENSITIVE);
//左匹配
Pattern pattern = Pattern.compile("^" + query + ".*$", Pattern.CASE_INSENSITIVE);
//模糊匹配
Pattern pattern = Pattern.compile("^.*" + query + ".*$", Pattern.CASE_INSENSITIVE);
Query query = Query.query(Criteria.where("字段名称").regex(pattern));

2、搜索不区分大小写

Criteria criteria = Criteria.where("type").regex(account, "i");// i表示不区分大小写

3、一个参数对应多个字段模糊匹配

 Criteria criteria = new Criteria().orOperator(
                    Criteria.where("字段1").regex(query),
                    Criteria.where("字段2").regex(query),
                    Criteria.where("字段3").regex(query));

4、多个参数对应一个字段模糊匹配
List<Criteria> criteriaLists = new ArrayList<>();
String[] types= params.split(","); // params=1,2,3,4,5
for (String type: types) {
    String regex = ".*?" + type + ".*?";
    criteriaLists.add(Criteria.where("type").regex(regex));
}

Criteria criteria = new Criteria();
criteria.orOperator(criteriaLists.stream().toArray(Criteria[]::new));
query.addCriteria(criteria);

5、单字段排序

query.with(new Sort(Sort.Direction.ASC, "name"));

6、多字段排序

 query.with(new Sort(Sort.Direction.ASC, "name").and(new Sort(Sort.Direction.ASC,"age")));

7、多个字段同时升序或降序

query.with(new Sort(Direction.ASC,"name","age","addr"));

8、查询字段值不为空的数据

query .addCriteria(Criteria.where("type").ne(null));

存的是空字符串,则使用:query .addCriteria(Criteria.where("type").ne(""));

9、查询排除字段和指定返回字段

Query query = new Query();

query.fields().include("path"); //包含该字段

query.fields().exclude("salary");//不包含该字段

10、查询返回某个字段的集合

mongoTemplate.findDistinct(new Query(), "age", "student", String.class)

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值