Search code examples
faunadb

Increment number while getting object from fauna db?


I am currently getting an object from my fauna database collection with an Index like so:

Get(
    Match(
        Index("byURL"),
        "rv49z1"
    )
)

I would also like to be able to increment a number variable (clicks) in the document I fetch each time I fetch it, ideally in one command.

Any help is greatly appreciated. Thank you.


Solution

  • Something like this should do the trick:

    Let(
      { o: Get(...) },
      Update(
        Select("ref", Var("o")),
        {
           data: {
             clicks: Add(1, Select(["data", "clicks"], Var("o")))
           }
        }
      )
    )