mongoTemplate使用in,not in进行条件查询,Sort排序查询,where条件,count使用,mongodb分页查询,日期$gte $lte查询

not in查询

ArrayList<String> list = new ArrayList<>();
list.add("1");
list.add("2");
query.addCriteria(Criteria.where("type").nin(list));

想对应的查询语句

db.log_record.find({"type":{"$nin":["1","2"]}})

in 查询

 db.log_record.find({"type":{"$in":["1","2"]}})

ArrayList<String> list = new ArrayList<>();

list.add("1");

list.add("2");

query.addCriteria(Criteria.where("type").in(list));
 

排序

query.with(new Sort(new Sort.Order(Sort.Direction.DESC,"create_time")));按create_time降序查询

db.log_record.find({"type":{"$in":["1","2"]}}).sort({create_time:-1})

where条件 

Criteria.where("status").is("success") 
查询where "status"="success"

count计数

 db.log_record.find().count();

long count = mongoTemplate.count(query, entityClass,collectionName);

mongodb分页查询

query.skip(offset);//offset偏移量,跳过offset条开始,从offset+1开始
query.limit(limit);//从offset+1条开始,查询limit条
List queryList = mongoTemplate.find(query,entityClass,collectionName);

db.log_record.find().skip(offset).limit(limit)

日期查询

db.log_record.find({"create_time":{$gte:ISODate("2019-06-25T00:00:00.000Z"),$lte:ISODate("2019-06-25T23:59:59.000Z")}})

查询日期,要用ISODate格式化下,不然查询不出数据。

一开始用下面的语句,一直查询不出来

db.log_record.find({"create_time":{$gte:new Date(2019,06,15),$lte:new Date(2019,06,29)}})

也不能直接查询,例如下面这样

db.log_record.find({"create_time":ISODate("2019-06-25T00:00:00.000Z")}) 这样也是查不出数据的

猜你喜欢

转载自blog.csdn.net/u012373281/article/details/93622529
今日推荐