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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With