Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb QueryByExample findOne

Some other process inserts documents into mongo collection and below is the sample data

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

I'm trying to grab one record for the clientId, using QueryByExampleExecutor.

Here is my model

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

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

}

Here is the code to build the example:

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);

This is not working , below is the generated query

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

Obviously the _class has the different package than the one in the mongo collection. How can I tell mongo to build a query without _class. I tried withIgnoredPaths and its not working.

like image 240
PKR Avatar asked Jan 24 '26 15:01

PKR


1 Answers

MongoExampleMapper inspects the probe type and writes type restrictions according to known types in MappingContext. Types assignable to the probe get included in the $in operator.

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" ] }
}

This behavior cannot be altered by using .withIgnorePaths() as the _class specifier is not part of the domain model. Please file an issue in jira.spring.io if you think this should be considered.

Looking at the provided sample data from the mongo collection you provided the _class attribute is not matching your domain type and therfore cannot be loaded.

like image 193
Christoph Strobl Avatar answered Jan 27 '26 04:01

Christoph Strobl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!