5

I am New to Cosmos DB. I am retrieving records using SQL API.now i want to add "Not In" in ARRAY_CONTAINS. I did lots of R&D for the same but not able to find out any solutions.

Mitul Suthar
  • 53
  • 1
  • 3

3 Answers3

4

You cannot add "Not In" in ARRAY_CONTAINS. There is not such syntax in Cosmos DB.

If you want to retrieving the records which donot contain some value. You can use NOT EXISTS. For below example

SELECT food.id,
    food.description,
    food.tags,
    food.foodGroup
FROM food 
WHERE NOT EXISTS(SELECT VALUE t FROM t IN food.tags WHERE t.name = 'orange')

Above query will retrive all the food records on the condition "orange" is not in tags. Below is an example from above query result.

{
  "id": "19015",
  "description": "Snacks, granola bars, hard, plain",
  "tags": [
    {
      "name": "snacks"
    },
    {
      "name": "granola bars"
    },
    {
      "name": "hard"
    },
    {
      "name": "plain"
    }
  ],
  "foodGroup": "Snacks"
}

You can go to the Query Playground of Cosmos DB to learn and practice Cosmos DB querying.

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
4

This will find all items where the property resolutions (which is an array) does not contain the value "600".

SELECT VALUE root FROM root WHERE (NOT ARRAY_CONTAINS(root["resolutions"], 600))
Niels Brinch
  • 3,033
  • 9
  • 48
  • 75
3

You can add NOT IN simply like below example:

SELECT
    food.id,
    food.description,
    food.tags,
    food.foodGroup
FROM 
  food    
WHERE food.id
NOT IN ('ID1', 'ID2', 'ID3')

This is tested and will work like a charm.

Krishan Pal
  • 306
  • 1
  • 3