'How can I write Spring Data Mongo repository method for listing document with embedded object parameters?

I wanna tell my problem with example. If we wannna find user by username, we can write findByUsername(String username); method and it works. But my parameter is not first layer property of my class.

@Data
@Document(collection = "stockItem")
public class StockItem implements Serializable {

    @Id
    private String id;
.
.
.
    @DBRef
    @Field
    private Stock stock;

 
}


@Data
@Document(collection = "stock")
public class Stock implements Serializable {

    @Id
    private String id;
.
.
.

    @DBRef
    @Field
    private Location location;
}

@Data
@Document(collection = "location")
public class Location implements Serializable {

    @Id
    private String id;

    @Field
    private String name;
.
.
}

I wanna list stockItems by locationId. So i have to write a query like findByStockLocationId(String locationId); in stockItemRepository. How can I write this query / aggregation or smt else? How can i access stockItem.stock.location.id ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source