Tuesday, November 26, 2019

Handling JSON document data types in Azure Cosmos DB


     Azure Cosmos DB is a NoSQL database and data is stored as JSON document. That means schema does not exist in database layer. Some of your JSON documents might have different objects or properties than others. Usually I get questions like :

  • How do you write a query if you schema changes from document to document? 
  • Do you need to check for null objects or properties in SQL queries?
  • Do you count objects first to be sure object is in JSON document?
     JSON documents do not have many data types and NoSQL Databases have no schema, you might find yourself parsing data or checking if data exists first in your queries. Azure Cosmos DB has some internal system functions you can use in these situations.

     I am going to use the following JSON document to demo the system functions.



IS_ARRAY(expression)
This function return True or False. Here is an example



IS_BOOL(expression)
This functions returns true or false. It checks if given value is a boolean or not.


IS_DEFINED(expression)
This function checks if the property is defined in Json document.


IS_NULL
This works just like ISNULL() in SQL Server. It check if given property is null.



IS_NUMBER
This function checks if given property's data type is numeric.


IS_OBJECT
This function check if given property is a custom object. In the following example, title exists but it is not an object.



IS_PRIMITIVE
This functon check is given property is one of the followings (string, bool, numberic, null)




IS_STRING
This function checks if given property is string.


No comments:

Post a Comment