I am trying to use findAll(Example<S> example)
of MongoRepository in order to filter for specific documents.
I have following nested data structure:
@Data
@Document
@Builder
@Jacksonized
public class House {
private List<Room> rooms;
private String name;
}
@Data
@Builder
@Jacksonized
public class Room {
private List<Chair> chairs;
}
@Data
@Builder
@Jacksonized
public class Chair {
private String color;
private Long countOfLegs;
}
Now I create two houses with corresponding rooms and chairs. Only house1 has an chair with 3 legs and only house2 has chairs with 5 legs.
I am trying to use findAll
with Examples
for this case:
@Test
@SneakyThrows
void contextLoads() {
final House house1hasChairWith3Legs = getHouse1();
final House house2hasChairWith5Legs = getHouse2();
final List<House> testData = List.of(house1hasChairWith3Legs, house2hasChairWith5Legs);
repository.insert(testData);
final ExampleMatcher matcher = ExampleMatcher.matchingAll().withIgnoreNullValues();
final House probe = House.builder()
.rooms(List.of(Room.builder().chairs(List.of(Chair.builder().countOfLegs(3L).build())).build()))
.build();
final Example<House> example = Example.of(probe, matcher);
final List<House> result = repository.findAll(example); // empty result
assertEquals(1, result.size());
}
result
is empty.
Logging the query shows me that following query is used:
{ "rooms" : [{ "chairs" : [{ "countOfLegs" : 3}]}] ... }
Regarding the warning in https://www.mongodb.com/docs/manual/tutorial/query-embedded-documents/#match-an-embedded-nested-document I would expect a query with dot notation, like: {"rooms.chairs.countOfLegs": 3}
. This query leads to the expected result.
Is there some configuration to use queries with dot notation? Is this expected behavior?
See https://github.com/the-bug/QueryByExampleExecutor---MongoDB/tree/master for playing around.
I was reading the specs of nested queries and tried option of the ExampleMatcher.