繁体   English   中英

Mongodb QueryByExample findOne

[英]Mongodb QueryByExample findOne

其他一些进程将文档插入到mongo集合中,下面是示例数据

{ "_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 }

我正在尝试使用QueryByExampleExecutor为clientId获取一条记录。

这是我的模特

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与mongo集合中的包具有不同的包。 如何告诉mongo在没有_class的情况下构建查询。 我试过withIgnoredPaths并且它不起作用。

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集合的提供的示例数据,您提供的_class属性与您的域类型不匹配,因此无法加载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM