Yes, it is less work to update a single document instead of searching for some number of documents to update.
Note that a reference is a unique identifier for a document in a database. There cannot be two documents sharing a reference. If you can provide a reference for a document that should be updated, then you can update it directly.
I’m guessing that you want to prevent users from guessing email addresses where content updates could be made (although that would be very weak security). You can certainly create a UDF that performs the update only when the email address matches.
Here is what I tried:
> CreateCollection({ name: "timer" })
{
ref: Collection("timer"),
ts: 1614712822980000,
history_days: 30,
name: 'timer'
}
> Create(Collection("timer"), { data: { email: 'me@me', score: 5 }})
{
ref: Ref(Collection("timer"), "291980201289581056"),
ts: 1614712868880000,
data: { email: 'me@me', score: 5 }
}
> Create(Collection("timer"), { data: { email: 'you@you', score: 11 }})
{
ref: Ref(Collection("timer"), "291980212836499968"),
ts: 1614712879890000,
data: { email: 'you@you', score: 11 }
}
> CreateFunction({
name: "updateTimer",
body: Query(
Lambda(
["timerRef", "email", "score"],
If(
Equals(
Var("email"),
Select(["data", "email"], Get(Var("timerRef")))
),
Update(Var("timerRef"), { data: { score: Var("score") }}),
Abort("Score update not permitted!")
)
)
)
})
{
ref: Function("updateTimer"),
ts: 1614713186500000,
name: 'updateTimer',
body: Query(Lambda(["timerRef", "email", "score"], If(Equals(Var("email"), Select(["data", "email"], Get(Var("timerRef")))), Update(Var("timerRef"), {data: {score: Var("score")}}), Abort("Score update not permitted!"))))
}
> Call(Function("updateTimer"), Ref(Collection("timer"), "291980201289581056"), "me@me", 47)
{
ref: Ref(Collection("timer"), "291980201289581056"),
ts: 1614713240880000,
data: { email: 'me@me', score: 47 }
}
> Call(Function("updateTimer"), Ref(Collection("timer"), "291980201289581056"), "me@you", 47)
Error: call error
{
errors: [
{
position: [],
code: 'call error',
description: 'Calling the function resulted in an error.',
cause: [
{
position: [
'expr',
'else'
],
code: 'transaction aborted',
description: 'Score update not permitted!'
}
]
}
]
}