1、符合单个、多个字段的计数
使用Repository方式,继承的MongoRepository中,有接口count,这个方法返回一个集合中所有Document的数量。
如果需要返回指定字段的数量,使用countBy方式,假设字段名叫做userId,那么这个方法就需要命名为:
long countByUserId(String userId);
这种方式是底层的mongo代码决定的。所以要符合规则。
如果是多个字段,都要满足。就需要用上And字眼,比如:
long countByUserIdAndSalary(String userId, int salary);
类似关键字And的字眼,可以是Or(或者)等等。
参考:
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.query