2017-07-28 106 views
2

其他一些过程将文档转换为蒙戈收集和下面是样本数据MongoDB的QueryByExample findOne

{ "_id" : ObjectId("597b89c8da52380b04ee6948"), "_class" : "com.test.mongo", "clientId" : "CAQ123999", "isValid" : false, "isParent" : true } 
{ "_id" : ObjectId("597b89c8da52380b04ee6949"), "_class" : "com.test.mongo", "clientId" : "CAQ123999", "isValid" : false, "isParent" : true } 
{ "_id" : ObjectId("597b89c8da52380b04ee6950"), "_class" : "com.test.mongo", "clientId" : "CAQ123998", "isValid" : true, "isParent" : true } 
{ "_id" : ObjectId("597b89c8da52380b04ee6951"), "_class" : "com.test.mongo", "clientId" : "CAQ123997", "isValid" : true, "isParent" : false } 

我试图抓住一个记录ClientID的,使用QueryByExampleExecutor。

这里是我的模型

package com.test.cfp.model; 
public class TFSModel { 

    private String clientId; 
    private boolean isValid; 
    private boolean isParent; 
    ... 

} 

这里是构建示例代码:

TFSModel tfs = new TFSModel(); 
      tfs.setClientId(CAQ123999); 
      tfs.setValid(false); 
      tfs.setParent(true);  
      ExampleMatcher matcher =ExampleMatcher.matching().withIgnoreNullValues().withIgnorePaths("_id","_class"); 
      Example<TFSModel > example = Example.of(tfs,matcher);   
      TFSModel oneTfsRecord = tflsRepository.findOne(example); 

这不是工作,下面是生成的查询

findOne using query: { "isValid" : false , "isParent" : true , "clientId" : "CAQ123999" , "_class" : { "$in" : [ "com.test.cfp.model.TFSModel"]}} in db.collection: returns.tfs; 

很明显,_class与mong中的不同o收藏。我如何告诉mongo在没有_class的情况下构建一个查询。我尝试过使用IgororedPaths,但它不工作。

回答

1

MongoExampleMapper检查探头类型并根据MappingContext中的已知类型写入类型限制。可分配给探针的类型包含在$in运营商中。

class One { 
    @Id String id; 
    String value; 
    // ... 
} 

class Two extends One { 
    // ... 
} 

One probe = new One(); 
probe.value = "firefight"; 

Example<One> example = Example.of(probe, ExampleMatcher.matchingAny()); 
{ 
    value: firefight, 
    _class: { $in : [ "com.example.One" , "com.example.Two" ] } 
} 

这种行为无法使用.withIgnorePaths()作为_class符不是域模型的一部分被改变。如果您认为应该考虑这个问题,请在jira.spring.io中提出问题。

查看您提供的属性与mongo集合提供的示例数据不匹配您的域类型,因此无法加载。

+0

谢谢@Christoph Strobl。我结束了使用MongoTemplate。我不确定它是否有效。无论如何,我会尝试在jira.spring.io中记录此信息。 – PKR