RSimple
RSimple

Reputation: 25

Cosmos DB query for null array like [null] object is present

I need to query from CosmosDB for items that has a null array of data like a [null].

I tried query like this query SELECT TOP 1 c.id from c where ARRAY_CONTAINS(c.data1, [[null]], true). My intent is to query something like this.

SELECT TOP 1 c.id from c where ARRAY_CONTAINS(c.data1, [[null]], true)

NO result is returned for the above query.

var sampledata = `{
  "id": 48259,
  "data": [
    [
      {
        "Elements": [
          {
            "QS": "",
            "TypeC": "C",
            "Id": 378,
            "R": false,
            "KTime": "0",
            "AllKVal": "",
            "KVal": "2",
            "IsGreen": false
          }
        ]
      }
    ],
    [
      null,
      {
        "Elements": [
          {
            "QS": "",
            "TypeC": "CM",
            "Id": 243,
            "R": "",
            "KTime": "0",
            "AllKVal": "",
            "V": true,
            "KVal": "2",
            "IsGreen": false
          }
        ]
      }
    ]
  ],
  "_ts": 1560943024
}`;

How data looks

Upvotes: 1

Views: 4276

Answers (1)

Jay Gong
Jay Gong

Reputation: 23782

I think i get your intention. Let us say, your data maybe like this:

{
    "id": "48259",
    "data": [
        [
            {
                "Elements": [
                    {
                        "QS": "",
                        "TypeC": "C",
                        "Id": 378,
                        "R": false,
                        "KTime": "0",
                        "AllKVal": "",
                        "KVal": "2",
                        "IsGreen": false
                    }
                ]
            }
        ],
        [
            null
        ],
        [
            null,
            {
                "Elements": [
                    {
                        "QS": "",
                        "TypeC": "CM",
                        "Id": 243,
                        "R": "",
                        "KTime": "0",
                        "AllKVal": "",
                        "V": true,
                        "KVal": "2",
                        "IsGreen": false
                    }
                ]
            }
        ]
    ]
}

data array has element is entire null array:

enter image description here

or has element contains null value and other values.

enter image description here

If so,you could use join in your sql to deal with both of them.

SELECT distinct c.id from c 
join data in c.data
where ARRAY_CONTAINS(c.data,[null],true) 
or ARRAY_CONTAINS(data,[null],true) 

Output:

enter image description here

Upvotes: 3

Related Questions