简体   繁体   English

使用 Spring Data Reactive MonogoDB 的表达式中具有多个表达式和多个字段的 $or 运算符

[英]$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中定义一个方法来查询包含field1field2的文档:

  1. There seems no proper keywords to create a query method ( findAllBy(field1AndField2)In ???)似乎没有合适的关键字来创建查询方法( findAllBy(field1AndField2)In ???)
  2. If using JSON-based Query Methods, I really do know how to complete the Cloze test...如果使用基于 JSON 的查询方法,我真的知道如何完成完形填空测试......
     @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 likeReactiveMongoTemplate中,您可以使用andor之类的操作创建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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM