【问题标题】:Spring data mongo db query on inner fields of embedded document (DBRef)Spring data mongo db查询嵌入式文档的内部字段(DBRef)
【发布时间】:2015-02-19 02:21:39
【问题描述】:

我有一个文档,其中包含另外两个文档的引用。我必须在 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());

【问题讨论】:

    标签: spring mongodb mongodb-query spring-data-mongodb


    【解决方案1】:

    您的查询看起来正确;您应该能够根据 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()));
    

    【讨论】:

    • 如果不是 dbref?
    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 2017-10-30
    • 2015-12-21
    • 2011-11-04
    • 2013-08-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多