3

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.

PKR
  • 228
  • 2
  • 16

1 Answers1

1

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.

Christoph Strobl
  • 6,491
  • 25
  • 33
  • Thanks @Christoph Strobl . I ended up using MongoTemplate. I'm not sure whether it is valid scenario or not. Anyway I'll try to log this in jira.spring.io. – PKR Jul 31 '17 at 15:42