Query CosmosDb - where array contains item(s) from array

前端 未结 3 786
执笔经年
执笔经年 2021-01-01 20:09

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 sou

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-01 21:10

    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 

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

提交回复
热议问题