SpringBoot中的MongoTemplate的各种条件查询示例详解
更新时间:2024年01月05日 14:43:00 作者:kenhins
这篇文章主要介绍了SpringBoot中的MongoTemplate的各种条件查询示例详解,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友参考下吧
SpringBoot之MongoTemplate的各种条件查询 (嵌套对象查询)
/** * 指定field查询 */ public void specialFieldQuery() { Query query = new Query(Criteria.where("user").is("用户名blog")); // 查询一条满足条件的数据 Map result = mongoTemplate.findOne(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | specialFieldQueryOne: " + result); // 满足所有条件的数据 List<Map> ans = mongoTemplate.find(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | specialFieldQueryAll: " + ans); } /** * 多个查询条件同时满足 */ public void andQuery() { Query query = new Query(Criteria.where("user").is("用户名blog").and("age").is(18)); Map result = mongoTemplate.findOne(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | andQuery: " + result); } /** * 或查询 */ public void orQuery() { // 等同于 db.getCollection('demo').find({"user": "用户名blog", $or: [{ "age": 18}, { "sign": {$exists: true}}]}) Query query = new Query(Criteria.where("user").is("用户名blog") .orOperator(Criteria.where("age").is(18), Criteria.where("sign").exists(true))); List<Map> result = mongoTemplate.find(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | orQuery: " + result); // 单独的or查询 // 等同于Query: { "$or" : [{ "age" : 18 }, { "sign" : { "$exists" : true } }] }, Fields: { }, Sort: { } query = new Query(new Criteria().orOperator(Criteria.where("age").is(18), Criteria.where("sign").exists(true))); result = mongoTemplate.find(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | orQuery: " + result); } /** * in查询 */ public void inQuery() { // 相当于: Query query = new Query(Criteria.where("age").in(Arrays.asList(18, 20, 30))); List<Map> result = mongoTemplate.find(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | inQuery: " + result); } /** * 数字类型,比较查询 > */ public void compareBigQuery() { // age > 18 Query query = new Query(Criteria.where("age").gt(18)); List<Map> result = mongoTemplate.find(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | compareBigQuery: " + result); // age >= 18 query = new Query(Criteria.where("age").gte(18)); result = mongoTemplate.find(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | compareBigQuery: " + result); } /** * 数字类型,比较查询 < */ public void compareSmallQuery() { // age < 20 Query query = new Query(Criteria.where("age").lt(20)); List<Map> result = mongoTemplate.find(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | compareSmallQuery: " + result); // age <= 20 query = new Query(Criteria.where("age").lte(20)); result = mongoTemplate.find(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | compareSmallQuery: " + result); } /** * 正则查询 */ public void regexQuery() { Query query = new Query(Criteria.where("user").regex("^用户名blog")); List<Map> result = mongoTemplate.find(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | regexQuery: " + result); } /** * 查询总数 */ public void countQuery() { Query query = new Query(Criteria.where("user").is("用户名blog")); long cnt = mongoTemplate.count(query, COLLECTION_NAME); System.out.println("query: " + query + " | cnt " + cnt); } /** * 查询总数 */ public void countQuery() { Query query = new Query(Criteria.where("user").is("用户名blog")); long cnt = mongoTemplate.count(query, COLLECTION_NAME); System.out.println("query: " + query + " | cnt " + cnt); } /** * 排序查询 */ public void sortQuery() { // sort查询条件,需要用with来衔接 Query query = Query.query(Criteria.where("user").is("用户名blog")).with(Sort.by("age")); List<Map> result = mongoTemplate.find(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | sortQuery " + result); } /** * 分页查询 */ public void pageQuery() { // limit限定查询2条 Query query = Query.query(Criteria.where("user").is("用户名blog")).with(Sort.by("age")).limit(2); List<Map> result = mongoTemplate.find(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | limitPageQuery " + result); // skip()方法来跳过指定数量的数据 query = Query.query(Criteria.where("user").is("用户名blog")).with(Sort.by("age")).skip(2); result = mongoTemplate.find(query, Map.class, COLLECTION_NAME); System.out.println("query: " + query + " | skipPageQuery " + result); }
mongodbTemplate 查询嵌套对象(嵌套对象属性为条件)
@ApiOperation("测试查询") @PostMapping("/getTestList") public Response<IPage<TicketResp>> getTestList(TicketVo ticket, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize) { if(null == pageNum || null == pageSize){ pageNum = 1;pageSize = 10; } IPage<TicketResp> page = new Page<>(pageNum,pageSize); page = ticketService.getMyTicketList(page,ticket); //return Response.ok(page); // Criteria criteria = Criteria.where("_id").is(ticket.getTicketKey()) // .and("ticketContent.createUser").is("110201010"); Criteria criteria = Criteria.where("ticketContent.createUser").is("110201010"); Query query = new Query(criteria); List<TicketInfo> objList = mongoTemplate.find(query, TicketInfo.class); return Response.ok(objList); // History history = null; // //封装对象列表查询条件 // List<AggregationOperation> commonOperations = new ArrayList<>(); // //1. 指定查询主文档 // MatchOperation match = Aggregation.match(Criteria.where("userId").is("2")); // commonOperations.add(match); // //2. 指定投影,返回哪些字段 // ProjectionOperation project = Aggregation.project("historyList"); // commonOperations.add(project); // //3. 拆分内嵌文档 // UnwindOperation unwind = Aggregation.unwind("historyList"); // commonOperations.add(unwind); // //4. 指定查询子文档 // MatchOperation match2 = Aggregation.match( // Criteria.where("historyList.hh").is("2")); // commonOperations.add(match2); // // //创建管道查询对象 // Aggregation aggregation = Aggregation.newAggregation(commonOperations); // AggregationResults<JSONObject> reminds = mongoTemplate // .aggregate(aggregation, "history", JSONObject.class); // List<JSONObject> mappedResults = reminds.getMappedResults(); // if (mappedResults != null && mappedResults.size() > 0) { // history = JSONObject // .parseObject(mappedResults.get(0).getJSONObject("historyList").toJSONString(), History.class); // } }
到此这篇关于SpringBoot中的MongoTemplate的各种条件查询的文章就介绍到这了,更多相关SpringBoot MongoTemplate条件查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
最新评论