I want to increment a scalar predicate with upsert operation.
If the counter predicate is missing in a record, upsert operation should be able to set initial value.
I cannot set initial value if predicate value is missing because there is no way to check if some variable is undefined.
What you actually did
upsert {
query{
q(func: uid(0x4e22)){
v as visit-count
incrementedValue as math(cond(v > 0, v+1, 1))
}
}
mutation {
set {
<0x4e22> <visit-count> val(incrementedValue) .
}
}
}
Why that wasn’t great, with examples
I am getting following exception. Error: t: : Expected a value variable in cond but missing.
There is no way to check if some variable is missing.
There should be a function isMissing(variable) to check if a variable is missing.
Sure this would be a good feature to work with true/false statements. Undefined should be identified as false by the parser. I believe that this would be useful in several other situations.
But for now, here’s a workaround for you.
Cheers.
upsert {
query{
targt as q(func: uid(0x2)) {
v as <visit-count>
check as count(visit-count)
incrementedValue as math(cond(check > 0, v+1, 1))
}
}
mutation {
set {
uid(targt) <visit-count> val(incrementedValue) .
}
}
}
Internally, this issue was considered reasonable. For this fact, I will add it to the back-log.
Anyone interested in this behavior in the variables, please give your thumbs up. In the main comment of that issue. So we understand the level of popularity and maybe prioritize it based on that.