Find by embeded object field value

Hello, can enyone help me how to use MongoTemplate to find documents by embeded object value?

I have a classes:

public class GroceryItem {
    private String id;

    private String name;
    private Integer quantity;
    private String category;

    private List<Product> products;

Embeded class:

public class Product {
    private String name;
    private List<Level> levels;


public class Level {
    private String name;

How can I find document by embeded product “name” or level “name” field?

It is not working with query:

Query query = new Query(Criteria.where("name").is(name)

With @Query annotation it works fine:

@Query(value = "{ 'quantity' : ?0, '' : ?1, '' : ?2 }")
Stream<GroceryItem> findByQuantityProdNameLevelName(int quantity, String prodName, String level);


Solution found with MongoClient and Filters class approach:

CodecRegistry pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),

        MongoCollection<GroceryItem> collection = MongoClients.create(dbConnString)
                .getCollection("groceryitems", GroceryItem.class);

        FindIterable<GroceryItem> items = collection.find(eq("", "level1"), GroceryItem.class);

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.