Skip to content

Query mapper does not use @Field field name when querying nested fields in combination with nested keywords [DATAMONGO-1406] #2319

Closed
@spring-projects-issues

Description

@spring-projects-issues

Hansel DOrnellas opened DATAMONGO-1406 and commented

we have a document class;

@Document(collection = "Computer")
public class Computer
{
   @Id
   private String _id;

   private String batchId;

  @Field("stat")
   private String status;

   @Field("disp")
   private List<Monitor> displays;

   //setters and getters
}

public class Monitor {
   @Field("res")
   private String resolution;

  // setters/getters
}

In MongoTemplate.java, the call to :

protected <S, T> List<T> doFind(String collectionName, DBObject query, DBObject fields, Class<S> entityClass,
			CursorPreparer preparer, DbObjectCallback<T> objectCallback)

DBObject mappedQuery = queryMapper.getMappedObject(query, entity);

resolves the fields to the input query to the ones in the @Field annotations, except for these in embedded arrays. So, in the example above, resolution fields in DBObject remains resolution. While, the status field resolves to stat.

Note the queries in the inner list, are setup as elemMatch.

The query submitted to mongo after getMappedObject is called:

{ "$and" : [ { "stat" : "A"} , { "disp" : { "$elemMatch" : { "$and" : [ { "resolution" : { "$ne" :  null }} , { "resolution" : { "$ne" : ""}}]}}}] , "batchId" : "5d0f1c53-92a2-48cb-8c84-1061769962c1"}

Which doesn't get any data, because there is no field called resolution (the field in mongo is res).
Note: The query input to getMappedObject is:

{ "$and" : [ { "status" : "A"} , { "displays" : { "$elemMatch" : { "$and" : [ { "resolution" : { "$ne" :  null }} , { "resolution" : { "$ne" : ""}}]}}}] , "batchId" : "5d0f1c53-92a2-48cb-8c84-1061769962c1"}

Notice the status and displays fields correctly get converted to the value in the @Field annotation.

The correct query from getMappedObject should be:

{ "$and" : [ { "stat" : "A"} , { "disp" : { "$elemMatch" : { "$and" : [ { "res" : { "$ne" :  null }} , { "res" : { "$ne" : ""}}]}}}] , "batchId" : "5d0f1c53-92a2-48cb-8c84-1061769962c1"}

This basically means that any queries that operate on fields (with a name different from the peristed name) in the inner list will fail.


Affects: 1.7.2 (Fowler SR2)

Referenced from: pull request #384, and commits 593e54f, 8925c73, 116dda6, f7ab448, a875124

Backported to: 1.9.3 (Hopper SR3), 1.8.5 (Gosling SR5)

1 votes, 1 watchers

Metadata

Metadata

Assignees

Labels

in: mappingMapping and conversion infrastructuretype: bugA general bug

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions