Azure Cosmos DB check if array in field is contained in search array

帅比萌擦擦* 提交于 2019-12-11 01:48:10

问题


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.

So, what I am looking for is, given the collection test containing the documents: {"id":1,"filters":[1,2]} {"id":2,"filters":[1,3]}

if I execute: db.test.find({"filters":{"$elemMatch":{$nin: [1,3]}}})

I get back: {"id":1,"filters":[1,2]}

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]}}}})

returns:

{"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

I also tried the $issubset on an aggregation , to no avail ( 0 results ) :

db.test.aggregate([{$match:{$issubset:["$filters",[1,3]]}}])

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?

  • List item Am I using mongoDB features not implemented in CosmosDB yet?

  • Is it CosmosDB bug?

Thank you!


回答1:


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) } ] }


来源:https://stackoverflow.com/questions/45033663/azure-cosmos-db-check-if-array-in-field-is-contained-in-search-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!