Spring ReactiveMongoTemplate查询带有嵌套对象的对象

huangapple go评论81阅读模式
英文:

Spring ReactiveMongoTemplate querying objects with embedded objects

问题

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

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

class B {
String id;
String name;
}

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

SELECT B WHERE A.id = idA and A.beesInA.id = idB

只有 A 类是一个带有相应的仓库 RepoAdocument,该仓库 extends ReactiveMongoRepository<A, String>

我应该如何使用 ReactiveMongoTemplateRepoA 编写查询,以根据上述条件返回 Mono<B>

同样地,如果使用上述条件找到一个 B 的实例,我应该如何更新它?

英文:

My model is an object with a collection of objects of a different type,

class A {
String id;
String field1;
Set&lt;B&gt; beesInA;
}

class B {
String id;
String name;
}

I want to find an instance of B whose id is idB which belongs to an instance of A whose id is idA i.e.

SELECT B WHERE A.id = idA and A.beesInA.id = idB

Only class A is a document with a corresponding repository RepoA that extends ReactiveMongoRepository&lt;A, String&gt;

How do I write a query using ReactiveMongoTemplate for RepoA that will return Mono&lt;B&gt; according to the aforementioned criteria?

Similarly how do I update an instance of B that is found using the above criteria?

答案1

得分: 2

感谢 Oliver Drotbohm答案,我成功获得了所需的结果。

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();

这个 帖子 包含了使用 ReactiveMongoTemplateReactiveMongoRepository 的完整信息。

英文:

Thanks to an answer from Oliver Drotbohm I was able to get the result I needed

Query query = Query.query(Criteria.where(&quot;id&quot;).is(&quot;idA&quot;).and(&quot;beesInA.id&quot;).is(&quot;idB&quot;));
query.fields().include(&quot;beesInA.$&quot;);

template.findOne(query, A.class).flatMapIterable(A::getBeesInA).single();

The update of a particular instance of B is very similar

Query query = Query.query(Criteria.where(&quot;id&quot;).is(&quot;idA&quot;).and(&quot;beesInA.id&quot;).is(&quot;idB&quot;));
Update update = new Update().set(&quot;beesInA.$&quot;, updatedBObject);

template.findAndModify(query,
					update,
					new FindAndModifyOptions().returnNew(true),
					A.class)
                    .flatMapIterable(A::getBeesInA)
                    .filter(b -&gt; b.getId().equalsIgnoreCase(&quot;idB&quot;)
                    .single();

This post has complete info on using ReactiveMongoTemplate with ReactiveMongoRepository

huangapple
  • 本文由 发表于 2020年9月22日 09:40:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64001949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定