[英]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.但是当我在列表中同时传递A和B时,我的回复中没有得到文档。
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.