Check if field exists in CosmosDB JSON with SQL - nodeJS
Solution 1
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)
Solution 2
If you want to know if a field exists you should use the IS_DEFINED("FieldName") If you want to know if the field's value has a value the FieldName != null or FieldName <> null (apparently)
I use variations of this in production:
SELECT c.FieldName
FROM c
WHERE IS_DEFINED(c.FieldName)
Solution 3
Add the NOT
operator in the SQL query to negate.
SELECT r.id, r.authToken.instagram,r.userName
FROM root r
WHERE NOT IS_DEFINED(r.abc)
to exclude all entries where the FieldName abc
doesn't exist.
Related videos on Youtube
JDT
Updated on July 09, 2022Comments
-
JDT almost 2 years
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 toif (r.abc) {}
I have tried using
WHERE r.abc IS NOT NULL
Thanks in advance
-
JDT almost 6 yearsdoes this not check for the string
null
- thus assuming"abc":"null"
does not exist? - Or at least that"abc": null
does not exist, rather than that"abc"
stricly exists? -
Nick Chapsas almost 6 yearsNo this is checking whether the value is null (not string "null", but just null) OR whether the property named
abc
is part of the json body. -
JDT almost 6 yearsah ok thanks - is there a way to strictly check if the property name is part of the json? Thus ignore if the value is null too?
-
Nick Chapsas almost 6 yearsNothing that I can find in predefined functions. You would have to write a UDF yourself. Even
IS_DEFINED()
will return if the value isnull
. -
JDT almost 6 yearsOk - thanks, I will just make sure that "abc" never contains null.
-
Kun Hu over 4 yearsIS_DEFINED is particularly useful to find documents that don't have some field populated, when the serialization ignores the null fields. In this case, IS_NULL doesn't work.
-
A.Rowan over 4 yearsI'm writing a query right now to give me a list of records which haven't had an analytic calculated on them. It's great.
-
Lee Z almost 3 yearsThis answer is incorrect. To determine if the field actually exists, you need to do what @ARowan said