I have a Posts collection in ArangoDb. I have views attribute in it. I want to update it by one when someone view the Post.
How I can increment the views attribute value by one with AQL in ArangoDB.
I have done above with below query in OrientDb.
update posts INCREMENT views = 1 where @rid = '#10:12'
You achieve this in ArangoDB using the UPDATE statement after a FILTER
to pick the post you want to modify:
Lets create a collection with some documents:
db._create('posts')
db.posts.save({views: 1, rid: '#10:12'})
db.posts.save({views: 1, rid: '#11:12'})
First we revalidate our FILTER
condition:
db._query(`FOR post IN posts FILTER post.rid == '#10:12' RETURN post`).toArray()
[
{
"_key" : "1282487",
"_id" : "posts/1282487",
"_rev" : "1282806",
"rid" : "#10:12",
"views" : 1
}
]
We check back for arithmetic calculations:
db._query(`FOR post IN posts FILTER post.rid == '#10:12'
RETURN post.views + 1`).toArray()
[1]
Now we know everything working properly we phrase the update statement:
db._query(`FOR post IN posts FILTER post.rid == '#10:12'
UPDATE post WITH {views: post.views + 1} IN posts`)
db.posts.toArray()
[
{
"_key" : "1282487",
"_id" : "posts/1282487",
"_rev" : "1282806",
"rid" : "#10:12",
"views" : 2
}
]