Can I update a FaunaDB document without knowing its ID?

You are very close! Update does require a ref. You can get one via your index though. Assuming your index has a default values setting (i.e. paging a match returns a page of refs) and you are confident that the there is a single match or the first match is the one you want then you can do Select(["ref"], Get(Match(Index("users_by_email"), "[email protected]"))) to transform your set ref to a document ref. This can then be passed into update (or to any other function that wants a document ref, like Delete).


Although Bens comments are correct, (that's the way you do it), I wanted to note that the error you are receiving is because you are missing a bracket here: "users_by_email"), "[email protected]"

The error is logical if you know that Index takes an optional database reference as second argument.

To clarify what Ben said: If you do this you'll get another error:

Update(
  Match(
    Index("accounts_by_email"), "[email protected]"
  ),
  { data: { email: "[email protected]"} }
)

Since Match could potentially return more then one element. It returns a set of references called a SetRef. Think of setrefs as lists that are not materialized yet. If you are certain there is only one match for that e-mail (e.g. if you set a uniqueness constraint) you can materialize it using Paginate or Get: Get:

Update(
  Select(['ref'], Get(Match(
      Index("accounts_by_email"), "[email protected]"
  ))),
  { data: { email: '[email protected]'} }
)

The Get returns the complete document, we need to specify that we require the ref with Select(['ref']..

Paginate:

Update(
  Select(['data', 0], 
    Paginate(Match(
      Index("accounts_by_email"), "[email protected]"
    ))
  ),
  { data: { email: "[email protected]"} }
)

Tags:

Faunadb