Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB: What difference does it make whether I set an attribute to "NULL": "true" or just omit it?

I have some items that I insert like this:

PutItem:

"TableName": "pets",
"Item": {
  "petName": {
      "S": "Cat"
    },
  "hairColor": {
      "S": "gray"
    },
  "nickName": {
      "S": "Kitty"
    }
 }

Sometimes however, the pets does not have a nickname. I'm pretty new to dynamoDB, and I can see, that I can handle this in two ways (at least):

1)

"TableName": "pets",
"Item": {
  "petName": {
      "S": "Cat"
    },
  "hairColor": {
      "S": "gray"
    },
  "nickName": {
      "NULL": "true"
    }
 }

2)

"TableName": "pets",
"Item": {
  "petName": {
      "S": "Cat"
    },
  "hairColor": {
      "S": "gray"
    }
 }

What difference will this make for me (e.g. when accessing the data afterwards)? And what is the best practice?

like image 756
Niels Kristian Avatar asked May 19 '16 14:05

Niels Kristian


Video Answer


1 Answers

Setting an attribute to "NULL" or omitting it is not the same.

If you do not set an attribute at all it obviously does not exist on the specific item. An attribute with the data type "NULL" however does exist but contains no value (similar to null in JSON).

E.g. when doing a comparison with the comparison operator NULL an attribute with the property "NULL" will actually be false. The naming here is very confusing but the behavior is quite well documented. For the comparison operator NULL it says:

This operator tests for the nonexistence of an attribute, not its data type. If the data type of attribute "a" is null, and you evaluate it using NULL, the result is a Boolean false. This is because the attribute "a" exists; its data type is not relevant to the NULL comparison operator.

like image 141
birnbaum Avatar answered Oct 02 '22 22:10

birnbaum