我有一个文档引用了另外两个文档。我必须根据 UserLogin 和 shopmaster 的 id 进行查询。我怎样才能做到这一点。请建议如何查询。
@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());
最佳答案
您的查询看起来是正确的;您应该能够根据 DBRef 的 _id
进行查询。这是因为 DBRef 将集合、id 和(有时)数据库存储在父文档中。但是,您可能需要将要比较的 ID 转换为 ObjectId
,如下所示:
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()));
关于嵌入式文档内部字段的 Spring Data Mongo DB 查询(DBRef),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27578057/