[英]$or operator with multiple expressions and multiple fields in an expression using Spring Data Reactive MonogoDB
In MongoDB, I can use $or[{key1:'value11', key2:'value12'}, {key1:'value21', key2:'value22'}, {key1:'value31', key2:'value32'}, ...]
to query several documents which matches at least one of the expressions in the $or
operator.在 MongoDB 中,我可以使用
$or[{key1:'value11', key2:'value12'}, {key1:'value21', key2:'value22'}, {key1:'value31', key2:'value32'}, ...]
来查询与$or
运算符中的至少一个表达式匹配的多个文档。 Then how the thing can be done using Spring Data Reactive MonogoDB?那么如何使用 Spring Data Reactive MonogoDB 来完成这件事?
In particular, I define a entity class as:特别是,我将实体 class 定义为:
@Document
public class MyDocument
{
@Id
private String id;
private String field1;
private String field2;
}
And then, the repository interface for the entity:然后,实体的存储库接口:
public interface MyDocumentRepository extends ReactiveMongoRepository<MyDocument, String>
The question now is how to define a method in MyDocumentRepository
to query the documents with field1
and field2
:现在的问题是如何在
MyDocumentRepository
中定义一个方法来查询包含field1
和field2
的文档:
findAllBy(field1AndField2)In
???)findAllBy(field1AndField2)In
???) @Query("{$or:[(:fields)]} Flux<MyDocument> findAllBy????(Flux<???> fields)
Spring Data MongoDB has support for ReactiveMongoTemplate
. Spring 数据 MongoDB 已支持
ReactiveMongoTemplate
。 In a repository, you can use this as a connection to MongoDB which can be used with @Autowire
.在存储库中,您可以将其用作与 MongoDB 的连接,它可以与
@Autowire
一起使用。
In ReactiveMongoTemplate
you can create Criteria
with and
and or
operation like在
ReactiveMongoTemplate
中,您可以使用and
和or
之类的操作创建Criteria
Query query = new Query();
query.addCriteria(
new Criteria().andOperator(
Criteria.where("field1").exists(true),
Criteria.where("field1").ne(false)
)
);
and this can be passed to MongoDB with the before created instance of ReactiveMongoTemplate
这可以通过之前创建的
ReactiveMongoTemplate
实例传递给 MongoDB
Flux<Foo> result = reactiveMongoTemplate.find(query, Foo.class);
Documentation for use of configuration of ReactiveMongoTemplate
if needed can be found here如果需要,可以在此处找到使用
ReactiveMongoTemplate
配置的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.