【问题标题】:Spring ReactiveMongoTemplate querying objects with embedded objectsSpring ReactiveMongoTemplate 使用嵌入对象查询对象
【发布时间】:2020-09-22 01:40:30
【问题描述】:

我的模型是一个包含不同类型对象集合的对象,

class A {
String id;
String field1;
Set<B> beesInA;
}

class B {
String id;
String name;
}

我想找到一个 id 为 idB 的 B 实例,它属于一个 id 为 idA 的 A 实例,即

选择 B WHERE A.id = idA 和 A.beesInA.id = idB

只有 A 类是 document,对应的存储库 RepoAextends ReactiveMongoRepository&lt;A, String&gt;

如何使用ReactiveMongoTemplateRepoA 编写一个查询,该查询将根据上述条件返回Mono&lt;B&gt;

同样,如何更新使用上述条件找到的 B 实例?

【问题讨论】:

    标签: java spring mongodb-query reactive-programming


    【解决方案1】:

    感谢来自Oliver Drotbohmanswer,我能够得到我需要的结果

    Query query = Query.query(Criteria.where("id").is("idA").and("beesInA.id").is("idB"));
    query.fields().include("beesInA.$");
    
    template.findOne(query, A.class).flatMapIterable(A::getBeesInA).single();
    

    B的特定实例的更新非常相似

    Query query = Query.query(Criteria.where("id").is("idA").and("beesInA.id").is("idB"));
    Update update = new Update().set("beesInA.$", updatedBObject);
    
    template.findAndModify(query,
                        update,
                        new FindAndModifyOptions().returnNew(true),
                        A.class)
                        .flatMapIterable(A::getBeesInA)
                        .filter(b -> b.getId().equalsIgnoreCase("idB")
                        .single();
    

    post 包含有关使用ReactiveMongoTemplateReactiveMongoRepository 的完整信息

    【讨论】:

      猜你喜欢
      • 2012-10-06
      • 2018-01-07
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多