Spring Data MongoDb - 如何使用" findAll"仅返回有效文档的方法

时间:2017-06-30 12:50:22

标签: spring mongodb validation document

我使用Spring数据mongo作为ORM来访问我的MongoDb。 我需要阅读一个MongoDb集合,其中包含我不管理内容及其有效性的文档。 我面临一个问题:文件并非全部有效,而且当我使用“发现所有”时方法,我只是抓住了一个例外。我希望找到所有有效的文件并拒绝无效的文件,而不是这种行为。

例如,我有一个带有原始布尔值的bean,在文档中,该字段设置为String类型。所以当我使用' findAll'我现在得到一个例外,但我希望得到所有有效文件的清单。 我的意思是我的集合中的某些文档由于类型错误而无效 - 无法在bean Java类型中强制转换。我只是使用一个布尔示例,但它也可以使用int,long,所有原始Java类型。 有一种方法可以使用Spring数据mongo吗?

非常感谢,

这里的堆栈:

java.lang.IllegalStateException:无法执行CommandLineRunner     在org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779)[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]     在org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760)[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]     在org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747)[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]     在org.springframework.boot.SpringApplication.run(SpringApplication.java:315)[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]     在org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]     在org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]     at hello.Application.main(Application.java:38)[classes /:na] 引起:java.lang.NullPointerException:null     at hello.Customer_Accessor_sbf1mo.setProperty(Unknown Source)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mapping.model.ConvertingPropertyAccessor.setProperty(ConvertingPropertyAccessor.java:58)〜[spring-data-commons-1.13.3.RELEASE.jar:na]     在org.springframework.data.mongodb.core.convert.MappingMongoConverter $ 1.doWithPersistentProperty(MappingMongoConverter.java:290)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.core.convert.MappingMongoConverter $ 1.doWithPersistentProperty(MappingMongoConverter.java:278)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:330)〜[spring-data-commons-1.13.3.RELEASE.jar:na]     在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:278)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:238)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:198)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:194)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:85)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.core.MongoTemplate $ ReadDbObjectCallback.doWith(MongoTemplate.java:2313)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1966)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1784)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1767)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:641)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:359)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:197)~ [spring-data-mongodb-1.10.3.RELEASE.jar:na]     在org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:51)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8.0_111]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_111]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_111]     在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_111]     在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504)〜[spring-data-commons-1.13.3.RELEASE.jar:na]     在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489)〜[spring-data-commons-1.13.3.RELEASE.jar:na]     在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)〜[spring-data-commons-1.13.3.RELEASE.jar:na]     在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]     在org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)〜[spring-data-commons-1.13.3.RELEASE.jar:na]     在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]     在org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)〜[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]     在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]     在org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)〜[spring-data-commons-1.13.3.RELEASE.jar:na]     在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]     在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)〜[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]     在com.sun.proxy。$ Proxy43.findAll(Unknown Source)〜[na:na]     at hello.Application.run(Application.java:58)[classes /:na]     在org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776)[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]     ...省略了6个常见帧

2 个答案:

答案 0 :(得分:0)

您可以使用WHERE子句创建自定义查询,该子句仅返回有效数据。

为此,您必须以这种方式创建ObjectRepositoryCustom.java界面:

@Repository
public interface ObjectRepositoryCustom {

    //Where "o.field" is the field you have to check to decide if it's valid or not
    @Query("SELECT o FROM Object o WHERE o.field = somethingThatValidsIt")
    public List<Object> customFindAll();

}

然后你进入ObjectRepository.java并以这种方式添加extends ObjectRepositoryCustom

//import your custom repository here
@Repository
public interface ObjectRepository extends JpaRepository<Object,Long>, ObjectRepositoryCustom {

}

注意,我使用了“对象”类型,因为我不知道您的班级名称。

然后你可以像这样调用你的查询方法。

private final ObjectRepository objectRepository;
List<Object> result = objectRepository.customFindAll();

我希望它有所帮助!

答案 1 :(得分:0)

感谢您上面的所有消息。

我找到了一种简单而强大的方法(当我要求findAll查询时拒绝无效文档)。

以下示例,其中包含名为Customer的bean。

DBCollection collection = mongoTemplate.getCollection("customer");
DBCursor cursor = collection.find();
while (cursor.hasNext()) { 
  DBObject obj = cursor.next();
  try {
    Customer instance = mongoTemplate.getConverter().read(Customer.class, obj);  
    // We have now the Customer instance 
  } catch(Exception exception) {
    System.err.println("ERROR: Cannot convert to Customer this DBObject " + obj);
  }
}

我希望它可以提供帮助!