Categories
azure azure-cosmosdb json sql

Check if field exists in CosmosDB JSON with SQL – nodeJS

I am using Azure CosmosDB to store documents (JSON).

I am trying to query all documents that contain the field "abc", and not return the documents that do not have the field "abc". For example, return the first object below and not the second

{
"abc": "123"
}
{
"jkl": "098"
}

I am trying to use the following code:

client.queryDocuments(
collectionUrl,
`SELECT r.id, r.authToken.instagram,r.userName FROM root r WHERE r.abc`
)

I assumed the above would check if abc exists similar to if (r.abc) {}

I have tried using WHERE r.abc IS NOT NULL

Thanks in advance

All you need to do is change your query to

SELECT r.id, r.authToken.instagram,r.userName FROM root r WHERE r.abc != null

or

SELECT r.id, r.authToken.instagram,r.userName FROM root r WHERE r.abc <> null

Both operators work (tested on the Data Explorer)