Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if field exists in CosmosDB JSON with SQL - nodeJS

Tags:

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

like image 267
JDT Avatar asked Sep 06 '18 11:09

JDT


People also ask

How do you query in Cosmos DB?

In the Azure Cosmos DB blade, locate and select the Data Explorer link on the left side of the blade. In the Data Explorer section, expand the NutritionDatabase database node and then expand the FoodCollection container node. Within the FoodCollection node, select the Items link. View the items within the container.

Is Cosmos DB SQL or NoSQL?

Cosmos DB is a multi-model NoSql database. Currently it can handle three types of non-relational data: Document databases.


1 Answers

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) 
like image 195
A.Rowan Avatar answered Oct 10 '22 22:10

A.Rowan