简体   繁体   中英

Azure Cosmos UDF to return default value if key doesnt exist

I have a document structure in Cosmos that typically looks like this:

{
    "Item No": "123456",
    "Item Desc": "This is a description.",
    "images": {
        "https://somedomain.com/image1.png",
        "https://somedomain.com/image2.png",
    }
}

Sometimes, there will be empty image values, for example:

{
    "Item No": "123456",
    "Item Desc": "This is a description.",
    "images": {
        "",
        ""
    }
}

I have written a UDF (user defined function) which will replace any empty values, with a default value:

function missingImage(doc, prop) {
  if (typeof doc[prop] === "undefined" || doc[prop] === "" || doc[prop] === null) {
    return "https://via.placeholder.com/150";
  }
}

In the event an image url is blank, I get back this return (correct):

{
    "id": "e3842b29-313c-4a84-bc94-bc43a9a55742",
    "Item No": "123456",
    "Item Desc": "This is a description.",
    "image": "https://via.placeholder.com/150"
},

My SELECT query looks like this:

"c.id, c['Item No'], c['Item Desc'], udf.missingImage(c.images[0]) as image"

However, in situations where no image key exists at all, for example:

{
    "Item No": "123456",
    "Item Desc": "This is a description."
}

I don't get back my default.

My question: How can I modify my UDF or query, such that if the images key does not exist, I still return a default value?

Firstly, the sample document you provided is incorrect format of json.

在此处输入图片说明

I suppose that it should be like:

在此处输入图片说明

You could modify the udf function like:

function missingImage(images) {
    for(var i =0;i<images.length;i++){
         if (typeof images[i] === "undefined" || images[i] === "" || images[i] === null) {
            images[i] = "https://via.placeholder.com/150";
        }
    }
    return images;
}

Then use sql to make sure no "" value in the results:

SELECT udf.missingImage(c.images) FROM c

在此处输入图片说明

Thanks @jay-gong for your time and response, however, this does not address the issue. I am looking for a way to return a default value when no images key exists in the document at all.

I feel the answer here, is not going to be through the UDF, rather it will need to be addressed at the query level. I am basing this off that fact that if I directly return a default, as you will see in the below UDF example, I don't get back images regardless.

The document in Cosmos:

{
    "id": "8fdc9f47-6209-455d-9b9c-482341bb3170",
    "Item No": "123456",
    "Item Desc": "This is a description."
}

The UDF:

function missingImage(images) {
  return "https://via.placeholder.com/150";
}

The query:

SELECT c.id, c['Item No'], c['Item Desc'], udf.missingImage(c.images) FROM c

The return:

[
    {
        "id": "8fdc9f47-6209-455d-9b9c-482341bb3170",
        "Item No": "123456",
        "Item Desc": "This is a description."
    }
]

UPDATE: I have come up with a solution, which is to use IS_DEFINED to check if the images key is defined. If its not, return false, which then gives me something to act on within the UDF.

The query:

SELECT c.id,
       c['Item No'],
       c['Item Desc'],
       udf.missingImage((IS_DEFINED(c.images) = true ? c.images : false)) 
FROM c

The UDF:

function missingImage(images) {
  if (images == false) {
    return "https://via.placeholder.com/150";
  }
  return images;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM