Reputation: 21
I am currently evaluating whether ArangoDB can be a future alternative for us. As a part of this evaluation I am porting code that talks to our current NoSQL db into code that speaks ArangoDB. While it has been a fairly smooth ride so far, I am having surprisingly difficult to wrap my head around on how to update sub documents. Assuming we have something like this:
{
"_key": "12345",
"subdoc": {
"0": {
"num_sold": 6,
"other_attribute": "important"
},
"1": {
"num_sold": 4,
"other_attribute": "important"
}
}
}
What I would like to accomplish now it to atomically increase num_sold
.
A very first naive approach was of course to try something similar to:
FOR d in @@collection
FILTER d._key == "12345"
UPDATE d WITH { subdoc.0.num_sold : subdoc.0.num_sold + 1 } IN @@collection
RETURN d
(Spoiler alert for the copy-pasters out there: move on. This snippet will just make your life miserable.)
This obviously didn't work and most likely for more than one reason. Arango does not seem to like me referencing the attribute using dot notation, the attribute starting with a number ("0") might also be an issue etc. While having found an example here it seemed both a bit complicated and convoluted for what I am trying to do. There is also another discussion here that is close to what I would like to do. However, the proposed solution in that discussion uses the keyword OLD
that creates an error in my case as well as the code replacing all keys in "0".
1) What is the best way to atomically increase num_sold
?
2) When is an operation atomic? (Trying to stay away from transactions as long as possible)
3) When can the dot notation be used and when can it not be used?
4) Can I bind parameters to an attribute? For instance letting some @attribute
be subdoc.0.num_sold
?
Thanks!
Upvotes: 2
Views: 597
Reputation: 6067
ArangoDB can't parse the query if you use numbers in the dot notation. However there is an easy way - simply use brackets instead of the dot notation as you did.
Example - not working:
db._query(`
LET testdoc = {subdoc: {"0": "abc"}}
RETURN testdoc.subdoc.0`)
ArangoError 1501: syntax error, unexpected integer number,
expecting identifier or bind parameter near '0' at
position 1:60 (while parsing)
Example - fixed:
db._query(`
LET testdoc = {subdoc: {"0": "abc"}}
RETURN testdoc.subdoc.[0]`)
[
"abc"
]
Using bind variables - not working:
db._query(`
LET testdoc = {subdoc: {"0": "abc"}}
RETURN testdoc.subdoc.@bv`, {bv: 0})
ArangoError 1501: syntax error, unexpected integer number,
expecting identifier or bind parameter near '0' at
position 1:60 (while parsing)
Using bind variables - fixed:
db._query(`
LET testdoc = {subdoc: {"0": "abc"}}
RETURN testdoc.subdoc.[@bv]`, {bv:0})
[
"abc"
]
Upvotes: 2