Azure Cosmos DB检查搜索数组中是否包含字段中的数组 [英] Azure Cosmos DB check if array in field is contained in search array
问题描述
我有一个Microsoft Azure CosmosDB MongoDB Api数据库,我正在尝试获取搜索数组中完全包含一个数组字段的所有文档.
I have an Microsoft Azure CosmosDB MongoDB Api database and I am trying to get all the documents where one array field is fully contained in my search array.
因此,考虑到包含文档的收集测试,我正在寻找的是:
{"id":1,"filters":[1,2]}
{"id":2,"filters":[1,3]}
So, what I am looking for is, given the collection test containing the documents:
{"id":1,"filters":[1,2]}
{"id":2,"filters":[1,3]}
如果我执行:
db.test.find({"filters":{"$elemMatch":{$nin: [1,3]}}})
我回来了:
{"id":1,"filters":[1,2]}
但是,如果我拒绝它,由于我希望所有带有过滤器的文档都完全包含在搜索中,因此将返回完整的文档列表.
db.test.find({"filters":{$not:{"$elemMatch":{$nin: [1,3]}}}})
However, if I negate it, since I want all the documents with filters fully contained in my search, the full list of documents is returned.
db.test.find({"filters":{$not:{"$elemMatch":{$nin: [1,3]}}}})
返回:
{"id":1,"filters":[1,2]}
{"id":2,"filters":[1,3]}
{"id":1,"filters":[1,2]}
{"id":2,"filters":[1,3]}
与文章中提到的相反: 检查数组中的每个元素是否都符合条件
contrary to what is mentioned in the article: Check if every element in array matches condition
我还尝试了聚合中的$ issubset,但无济于事(0个结果):
I also tried the $issubset on an aggregation , to no avail ( 0 results ) :
db.test.aggregate([{$match:{$issubset:["$filters",[1,3]]}}])
我什至尝试了(0个结果):
I even tried ( 0 results ):
db.test.aggregate([{$match:{$issubset:[[1],[1,3]]}}])
知道她发生了什么事的人吗?
Anyone with an idea of what is happening her?
-
这是我的错误吗?
Is it my error?
列表项我是否正在使用CosmosDB中尚未实现的mongoDB功能?
List item Am I using mongoDB features not implemented in CosmosDB yet?
是CosmosDB错误吗?
Is it CosmosDB bug?
谢谢!
推荐答案
在我身边测试$ nin时遇到服务器错误.然后,我尝试使用$ and组合多个子查询,并且效果很好.请使用以下代码查询包含完全包含在搜索中的过滤器的所有文档.
I encountered a server error when testing $nin on my side. Then I tried to use $and to combine multi sub queries and it worked fine. Please using following code to query all the documents with filters fully contained in your search.
{ "filters": NumberInt(1), $and: [ { "filters": NumberInt(3) } ] }
这篇关于Azure Cosmos DB检查搜索数组中是否包含字段中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!