Query query = new Query(); query.addCriteria(Criteria.where("name").is("Neo")); List<User> users = mongoTemplate.find(query, User.class);
查詢以N開頭的名字
Query query = new Query(); query.addCriteria(Criteria.where("name").regex("^N")); List<User> users = mongoTemplate.find(query,User.class);
查詢以o結尾的名字
Query query = new Query(); query.addCriteria(Criteria.where("name").regex("o$")); List<User> users = mongoTemplate.find(query, User.class);
查詢年齡小於 < 30 並 > 20 的用戶
Query query = new Query(); query.addCriteria(Criteria.where("age").lt(30).gt(20)); List<User> users = mongoTemplate.find(query,User.class);
實現一個區間條件 new Criteria("createdDate").gte(beginDate).lte(endDate)
public boolean AccountDeposit(Date beginDate, Date endDate) { MatchOperation matchOperation = match(new Criteria("createdDate").gte(beginDate).lte(endDate)); GroupOperation groupOperation = group("loginname").sum("amount").as("amount"); SortOperation sortOperation = sort(new Sort(Direction.ASC, "loginname")); Aggregation aggregation = newAggregation(matchOperation, groupOperation, sortOperation); AggregationResults<AccountSettlementDetails> results = mongoTemplate.aggregate(aggregation, AccountSettlementDetails.class, AccountSettlementDetails.class); if (results.getMappedResults() != null) { log.info(results.getRawResults().get("result").toString()); for (AccountSettlementDetails settlementDetails : results.getMappedResults()) { log.info("{}", settlementDetails.toString()); } } return true; }
Query query = new Query(); query.addCriteria( new Criteria().andOperator( Criteria.where("field1").exists(true), Criteria.where("field1").ne(false) ) ); List<Foo> result = mongoTemplate.find(query, Foo.class); System.out.println("query - " + query.toString()); for (Foo foo : result) { System.out.println("result - " + foo); }
按照年齡排序
Query query = new Query(); query.with(new Sort(Sort.Direction.ASC, "age")); List<User> users = mongoTemplate.find(query,User.class);
final Pageable pageableRequest = new PageRequest(0, 2); Query query = new Query(); query.with(pageableRequest);
MultilevelDirectSellingAccountRewardsSettlementDetails multilevelDirectSellingAccountRewardsSettlementDetails = new MultilevelDirectSellingAccountRewardsSettlementDetails(); multilevelDirectSellingAccountRewardsSettlementDetails.setLoginname("111"); multilevelDirectSellingAccountRewardsSettlementDetails.setPhone("111"); multilevelDirectSellingAccountRewardsSettlementDetails.setRecommenderLoginname("111"); multilevelDirectSellingAccountRewardsSettlementDetails.setRecommenderPhone("111"); multilevelDirectSellingAccountRewardsSettlementDetails.setRecommenderName("Neo"); multilevelDirectSellingAccountRewardsSettlementDetails.setRecommenderType("客戶"); multilevelDirectSellingAccountRewardsSettlementDetails.setAmount(5.02); multilevelDirectSellingAccountRewardsSettlementDetails.setCreatedDate(new Date()); multilevelDirectSellingAccountRewardsSettlementDetailsRepository.save(multilevelDirectSellingAccountRewardsSettlementDetails); Date beginDate = this.getToday("00:00:00"); Date endDate = this.getToday("23:59:59"); log.info(beginDate.toString() + " ~ " + endDate.toString()); GroupOperation groupOperation = group("loginname").sum("amount").as("amount"); MatchOperation matchOperation = match(new Criteria("createdDate").gte(beginDate).lte(endDate)); SortOperation sortOperation = sort(new Sort(Direction.ASC, "loginname")); Aggregation aggregation = newAggregation(matchOperation, groupOperation, sortOperation); AggregationResults<MultilevelDirectSellingAccountRewardsSettlementDetails> results = mongoTemplate.aggregate(aggregation, MultilevelDirectSellingAccountRewardsSettlementDetails.class, MultilevelDirectSellingAccountRewardsSettlementDetails.class); System.out.println(results.getRawResults().get("result").toString());