Spring Data with MongoDB (三)

Spring Data with MongoDB

节选自《Netkiller Spring Cloud 手札》

MongoRepository

扫描仓库接口

默认不需要设置,除非你的包不在当前包下,或者命令不是 repository。

@EnableMongoRepositories(basePackages = "cn.netkiller.repository") 

findAll()

 @RequestMapping(value = "read", method = RequestMethod.GET, produces = { "application/xml", "application/json" })
 @ResponseStatus(HttpStatus.OK)
 public List<Withdraw> read() {
 return repository.findAll();
 }

deleteAll()

repository.deleteAll();

save()

repository.save(new City("Shenzhen", "China"));

count()

 @RequestMapping("count")
 public long count() {
 return repository.count();
 }

exists() 判断是否存在

boolean isExists = userRepository.exists(user.getId()); 

existsById()

memberRepository.existsById(id); 

findByXXXX

List<User> findByName(String name);
List<User> users = userRepository.findByName("Eric");

findAll with OrderBy

order by boolean 布尔型数据排序

因为 boolean 数据 true = 1, false = 0 所以 ASC false 会排列在前面。所有很多时候而我们需要 DESC 排序

List<ShippingAddress> shippingAddress = shippingAddressRepository.findAllByMemberIdOrderByDefaultsDesc(memberId); 

findAll with Sort

List<User> users = userRepository.findAll(new Sort(Sort.Direction.ASC, "name")); 

FindAll with Pageable

Pageable pageable = PageRequest.of(0, 1);
Page<User> page = userRepository.findAll(pageable);
List<User> users = pages.getContent(); 

PageRequest - springboot 1.x 旧版本

Page<User> findByLastname(String lastname, Pageable pageable); 
 @RequestMapping(value = "read/{size}/{page}", method = RequestMethod.GET, produces = { "application/xml", "application/json" })
 @ResponseStatus(HttpStatus.OK)
 public List<Withdraw> readPage(@PathVariable int size, @PathVariable int page){
 PageRequest pageRequest = new PageRequest(page-1,size);
 return repository.findAll(pageRequest).getContent();
 }

URL翻页参数,每次返回10条记录

 第一页 http://localhost:8080/v1/withdraw/read/10/1.json
 第二页 http://localhost:8080/v1/withdraw/read/10/2.json
 ...
 第五页 http://localhost:8080/v1/withdraw/read/10/5.json

StartingWith 和 EndingWith

List<User> findByNameStartingWith(String regexp);
List<User> findByNameEndingWith(String regexp);

List<User> users = userRepository.findByNameStartingWith("N");
List<User> users = userRepository.findByNameEndingWith("o");

Between

数值范围

List<User> findByAgeBetween(int ageGT, int ageLT);

List<User> users = userRepository.findByAgeBetween(20, 50);

日期范围,取值 e.g. 2018-07-04 00:00:00 and 2018-07-04 23:59:59

List<Member> findByCreatedDateBetween(DateTime start, DateTime end); 

List<Member> findByCreatedDate(@Temporal(TemporalType.DATE) Date date); 

Before / After

List<Assets> findAllByUpdateDateBefore(Date yesterday);
List<Assets> findAllByUpdateDateBeforeAndStatus(Date yesterday, String status);

List<Assets> findAllByUpdateDateAfter(Date yesterday);

@Query

public interface PersonRepository extends MongoRepository<Person, String> {
 @Query("{ 'name' : ?0 }")
 List<Person> findWithQuery(String userId);
}

 @Query(value = "{'statusHistories':{$elemMatch:{'status':{$in:['PROCESSABLE']}}},'created' : { '$gt' : { '$date' : ':?0' } , '$lt' : { '$date' : ':?1'}}}", count = true)
 Long countMe(@Param("dateFrom") Date datefrom, @Param("dateTo") Date dateTo);
  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

netkiller-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值