57

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

3 Answers 3

87

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)
2
  • 2
    IS_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.
    – Kun Hu
    Dec 4, 2019 at 17:07
  • I'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.
    – A.Rowan
    Mar 11, 2020 at 19:30
28

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)

7
  • does 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?
    – JDT
    Sep 6, 2018 at 15:08
  • No 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. Sep 6, 2018 at 15:14
  • ah 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?
    – JDT
    Sep 6, 2018 at 15:16
  • 2
    Nothing that I can find in predefined functions. You would have to write a UDF yourself. Even IS_DEFINED() will return if the value is null. Sep 6, 2018 at 15:38
  • Ok - thanks, I will just make sure that "abc" never contains null.
    – JDT
    Sep 6, 2018 at 15:42
21

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 include all entries where the FieldName abc doesn't exist.

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.