简体   繁体   English

如果文档的数组中存在所提供列表的任何值,则过滤文档

[英]Filter the document if any value of the provided list is present in the document's array

Document文档

    {
    "status": {
            "active": [
                "A",
                "B"
            ],
            "inactive": [
                "C",
                "D"
            ]
        }
    }

Code代码

Criteria statusFilterCriteria = Criteria.getInstance(
   CriteriaType.ARRAY_CONTAINS,
   "status.active",   
   Collections.singletonList("A"), 
   Part.IgnoreCaseType.NEVER);
CosmosQuery cosmosQuery = new CosmosQuery(statusFilterCriteria);

The filtering criteria is that if any value of the list is present in the active array, then the Document should be returned.过滤标准是,如果列表的任何值存在于活动数组中,则应返回 Document。 Since A is present in active array I get A in response.由于A存在于活动数组中,因此我得到 A 作为响应。 But when I pass A and B both in the list, I don't get the Document in my response.但是当我在列表中同时传递AB时,我的回复中没有得到文档。

You can only pass a single value because the ARRAY_CONTAINS function in Cosmos DB backend itself only supports searching for a single value in the json array (see documentation here ).您只能传递单个值,因为 Cosmos DB 后端中的 ARRAY_CONTAINS 函数本身仅支持在 json 数组中搜索单个值(请参阅此处的文档)。 I realise this is misleading in the context of Spring and Criteria.getInstance(), since this is expecting List<Object> values, so the natural assumption you made is that it will search for all the values in the list... but you can only pass one value.我意识到这在 Spring 和 Criteria.getInstance() 的上下文中具有误导性,因为这是期望List<Object>值,所以你做出的自然假设是它将搜索列表中的所有值......但是你只能传递一个值。

To accomplish this, you will need to run the query each time for each value in the list.为此,您需要每次对列表中的每个值运行查询。 Something like the below snippet, which assumes there are multiple documents of this shape in your container, and collects all the docs that meet the filtering criteria into a HashMap.类似于下面的代码片段,假设您的容器中有多个这种形状的文档,并将所有符合过滤条件的文档收集到一个 HashMap 中。

Iterable<User> results = null;
String[] lettersArray = { "A", "B" };
HashMap<String, User> docMap = new HashMap<String, User>();
List<String> letters = Arrays.asList(lettersArray);
for (String object : letters) {
    Criteria statusFilterCriteria = Criteria.getInstance(
            CriteriaType.ARRAY_CONTAINS, "status.active", Collections.singletonList(object),
            Part.IgnoreCaseType.NEVER);
    CosmosQuery cosmosQuery = new CosmosQuery(statusFilterCriteria);
    results = cosmosTemplate.find(cosmosQuery, User.class, "myContainer5");
    for (User user : results)
        docMap.put(user.getId(), user);
}
List<String> docIds = new ArrayList<String>();
for (String id : docMap.keySet())
    docIds.add(id);
System.out.println("ids of docs that contain any of the letters: " + String.join(", ", docIds));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于来自同一集合的所有文档的数组对象中存在的 id 过滤列表的 MongoDb 聚合 - MongoDb aggregation for filtering list based on ids present in object of array from all document of same collection ColumnText中的列表丢失了直接添加到文档中时存在的数据 - List in ColumnText is loosing data that is present when added to the document directly 是否可以在mongo文档的嵌入式数组文档中添加对象列表? - Is it possible to add list of objects into embedded array document in mongo document? MongoDB-更新文档列表中的对象 - MongoDB - Update objects in a document's List 以DBObject(s)的形式检索数组中的子文档 - Retrieve sub-document in array as DBObject(s) 如何查询 Firebase 文档中的数组列表? - How to query an array list in a Firebase document? 在Java的MongoDB中修改嵌套文档的值 - Modify nest document's value in MongoDB for Java 在Spring框架的子文档数组字段中过滤数组 - Filter array in sub-document array field in Spring framework 列出 mongodb 中的所有文档,其中每个文档的一个字段在 spring 引导中具有最大值 - List all documents from mongodb where the each document's one field has max value in spring boot 带有文档过滤器的 Java 正则表达式 - Java REGEX With Document Filter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM