38

I don't know if there is a word for this, guess there is, but right now I couldn't explain it better than "where array contains item(s) from array".

It might sound weird, but actually it not (I think), and I'm having a hard time figuring out how I can do this in Azure CosmosDB.

Here goes. I have a document like this (simplified):

{
"id": "2a62fcf4-988f-4ebe-aedc-fb0c664b85d8",
"Title": "Seks års fængsel for overgreb",    
"ZipCodes": [
    {
        "Code": "6500",
        "Name": "Vojens",
        "FoundViaTerm": "Vojens"
    },
    {
        "Code": "6400",
        "Name": "Sønderborg",
        "FoundViaTerm": "Sønderborg"
    },
    {
        "Code": "6700",
        "Name": "Esbjerg",
        "FoundViaTerm": "Esbjerg"
    }
],
"_rid": "k1sVAPf7SQAMAAAAAAAAAA==",
"_self": "dbs/k1sVAA==/colls/k1sVAPf7SQA=/docs/k1sVAPf7SQAMAAAAAAAAAA==/",
"_etag": "\"00001000-0000-0000-0000-5a14898e0000\"",
"_attachments": "attachments/",
"_ts": 1511295374

}

Ok, now I want to query documents like this and find all, where ZipCodes.Code is in a list of zipcodes, ex. ('6500', '2700').

I'm puzzle here...

I found the ARRAY_CONTAINS method and it works, if I only come in with one zipcode - my problem is I come with a list.

Hope somebody can help, thanks in advance.

1

4 Answers 4

26

Per my experience , expr in ARRAY_CONTAINS (arr_expr, expr [, bool_expr]) method is not supported list arguments.

According to your situation , I suggest you use UDF in Cosmos DB.

I created 3 sample documents as your description.

[
  {
    "id": "1",
    "zip": [
      {
        "code": "1111"
      },
      {
        "code": "2222"
      }
    ]
  },
  {
    "id": "2",
    "zip": [
      {
        "code": "2222"
      },
      {
        "code": "3333"
      }
    ]
  },
  {
    "id": "3",
    "zip": [
      {
        "code": "4444"
      },
      {
        "code": "1111"
      },
      {
        "code": "2222"
      }
    ]
  }
]

Please refer to the snippet of UDF code as below :

function test(zipcode){
    var arrayList = ["1111","2222"]
    var ret = false ;
    for(var i=0 ;i <zipcode.length;i++){
        if(arrayList.indexOf(zipcode[i].code)){
            ret= true;
        }else{
            ret = false;
            break;
        }
    }
    return ret;
}

You could select zip array (select c.zip from c) ,then loop the results and invoke the UDF above in your code with the zip[i] arguments.

Hope it helps you.


Just for summary:

Use the IN operator from Cosmos DB SQL APIs to query entry which is included in the list condition.

Like

SELECT * FROM c WHERE c.ZipCodes[0].Code IN ("6500", "6700")

Or

SELECT DISTINCT c FROM c JOIN zc IN c.ZipCodes WHERE zc.Code IN ("2720", "2610")
15
  • Thank you for you response. I don't really get how this should actually help me, but you made me think more about UDF. Maybe that's part of the solution I need to look more into. Nov 27, 2017 at 20:05
  • 3
    Well, yes and no. I haven't had time to really implement it in code (C# that is) yet, but I have played around in the Data Explorer, and it seems I can use multiple ARRAY_CONTAINS with OR in between :) Actually as suggested by @Amitbe link. In that link it's also suggested to use the JOIN, I will look into that too and see if that better. These UDF - I really don't like it. Yes maybe for something totally unique and custom for my application, but really, what I need here isn't that unique or custom. Plus the fact that I REALLY don't like JavaScript. Dec 18, 2017 at 11:39
  • 1
    I tried using this just now, and it appears to be broken for any lists with a length greater than 1... Feb 1, 2018 at 23:51
  • 1
    @JayGong the UDF link which you provided is not valid any more :(
    – fascynacja
    Mar 30, 2020 at 13:44
  • 1
    @fascynacja It's appreciate to your notification! Already fix it!
    – Jay Gong
    Mar 30, 2020 at 16:22
12

I would like to propose another solution to this problem.
Use EXISTS with ARRAY_CONTAINS in this way:

SELECT * FROM c
WHERE EXISTS 
(SELECT VALUE z FROM z in c.ZipCodes WHERE ARRAY_CONTAINS(["6500","6700"], z))
5

You can do something like this: For each item in ZipCodes, you get a zip and compare with the array of codes you are checking. This, IMHO, is much better than using UDF.

{
  query: '
         SELECT DISTINCT value r
         FROM root r
         JOIN zip IN r.zipCodes
         WHERE ARRAY_CONTAINS(@zipIds, zip, true)
         ',
  parameters: [{name: "@zipIds", value: zipIds}]
}

The last param of ARRAY_CONTAINS tells the function to accept partial matches.

1
  • This query will have better performance: SELECT r FROM r JOIN (SELECT VALUE zip FROM zip IN r.zipCides WHERE zip IN (@zipIds))
    – Praytic
    Jun 28, 2020 at 20:29
3

Apart from the fact that using UDF looks as the easier option, i would not use UDFs in your query's filter, since it compromises the performance of your query. I faced the same problem in my work environment, where things are designed to use UDFs to help in the queries, but the reality is that most of the times we are doing queries by using single values, and using UDF will actually result on the query not using the index. So in that case if you need to validate multiple values in the array, depending on the volume of values you need to validate, you can always write something like ARRAY_CONTAINS(c, 1) or ARRAY_CONTAINS(c, 2) or ....

It doesn't look so elegant solution, but will ensure that it will use the index and will do the best performance in your query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.