如何处理来自findBy的返回对象.... Spring-Data-MongoDB中是否存在(boolean exists)?

时间:2017-01-29 14:07:48

标签: mongodb spring-boot spring-data-mongodb

我在MongoDB中有一个名为' customers'我存储具有不同模式的文档。

在我的Spring Boot应用程序中,我有以下存储库:

public interface BgradeCustomerRepository extends MongoRepository<BgradeCustomer, String> {
    public List<BgradeCustomer> findByScoreExists(boolean exists);
} 

当我调用findByScoreExists并传递true时,它工作正常,我得到了具有属性&#39;得分的对象集合,但问题是当我将false传递给findByScoreExists时,我得到&#的集合39; BgradeCustomer&#39;只与其他模式具有共同属性的对象!!

我将方法签名更改为:

// Customer is the other schema in the collection 
public List<Customer> findByScoreExists(boolean exists);

但从未奏效,我总是得到&#39; java.lang.NullPointerException:null&#39;

那么,我怎样才能获得所有没有特定属性的对象?

1 个答案:

答案 0 :(得分:1)

使用java.util.Optional包装对象 然后使用isPresent()方法进行检查