I am using spring-data-mongo and trying to access the dbref objects with params. My project looks like this:
My models are as follows:
i. First Document is "Cars"
@Document("cars")
class CarDocument {
@Id
private String id;
private String name;
private String madeInCountry;
private String model;
private String madeInYear;
}
ii. Second document is "tools"
Document("tools")
class ToolDocument {
@Id
private String id;
private String name;
private String madeInCountry;
private String madeInYear;
private List<UsedIn> usedIn = new ArrayList<>();
}
iii. The third is embedded model "UsedIn" in (ii.) Third embedded model represents where tools are used to make cars in the manufacturing house.
class UsedIn {
@DBRef
private CarDocument car;
private DateTime usedDate;
private String usedByUsername;
}
My DAO's are as follows:
public interface CarDAO extends MongoRepository<CarDocument, String>
{
public CarDocument findByMadeInCountry(String madeInCountry);
}
public interface ToolDAO extends MongoRepository<ToolDocument, String>
{
public ToolDocument findByMadeInCountry(String madeInCountry);
}
Now I need list of all the "Tools" which is used in the specific car. Say a. when car is madeInCountry: "germany" and b. tool is madeInCountry: "germany"
I see that we can't apply search directly on DBRef documents. like :
String madeInCountry = "germany";
toolDAO.findByMadeInCountryAndUsedInCarMadeInCountry(madeInCountry,madeInCountry);
I get this error:
"Invalid path reference car.madeInCountry! Associations can only be pointed to directly or via their id property!"
How to this?
Do I need to do two DAO calls? Say i. first get all the cars with madeInCountry is germany
String madeInCountry = "germany";
carDAO.findByMadeInCountry(madeInCountry);
ii. findTools by the list of carDocuments and String.
I dont know, how to call this dao with list of CarDocuments and madeInCountry String ?
Do I need to use some $lookup functionality?
Thanks