3

I have a a document which is having references of two other document. I have to query in the basis of id of UserLogin and shopmaster. How I can achieve this. Please suggest how to query.

@Id
private String userShopAssociationId;

@DBRef
private UserLogin userLogin;

@DBRef
private ShopMaster shopMaster;

Query query = new Query();
query.addCriteria(Criteria.where("userLogin.$id").is(userShopAssociationForm.getUserLoginId()));
query.addCriteria(Criteria.where("shopMaster.$id").is(userShopAssociationForm.getShopMasterId());

1 Answer 1

5

The queries that you have look correct; you should be able to query based on the _id of a DBRef. That's because a DBRef stores the collection, id, and (sometimes) the database on the parent document. However, you might need to convert the ids you are comparing to an ObjectId like so:

Query query = new Query();
query.addCriteria(Criteria.where("userLogin.$id").is(new ObjectId(userShopAssociationForm.getUserLoginId())));
query.addCriteria(Criteria.where("shopMaster.$id").is(new ObjectId(userShopAssociationForm.getShopMasterId()));
Sign up to request clarification or add additional context in comments.

1 Comment

and if not a dbref?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.