Learn about the new Firestore "increment" field-value that can update a counter on the server atomically, with dramatically simplified code https://fireship.io/snippets/firestore-increment-tips/

- Increment blog post https://firebase.googleblog.com/2019/03/increment-server-side-cloud-firestore.html
- Database Transactions https://firebase.google.com/docs/firestore/manage-data/transactions
2024-02-04T06:50:57.304Z
nova-2
2-general-nova
2024-01-09.29447
30089e05-99d1-4376-b32e-c263170674af
f263529a-da36-40d0-834f-a4c14d660296
14fb6c375537d0ae8af72aac94d2987f56640e8f99d3b1ad1140c4163afdfe5d
67875a7f-c9c4-48a0-aa55-5bdb8a91c34a
deprecated
One of the challenges with Firestore is keeping an accurate count on a document because you might have multiple clients competing to update the same value at the same time.
Firestore has always had a way to maintain accurate counters in the form of database transactions, but app, but they require a pretty significant amount of code just to do something simple like update a counter.
Fortunately, just a couple weeks ago Firestore released a new tool called Increment.
App.
In today's quick video, you'll learn how increment can help simplify your code and do things like data aggregation entirely from the front end.
If you're new here, like and subscribe, and you can grab the full source code from Fireship I IO.
Imagine you're in a situation where you have a blog post and you wanna keep track of the total number of likes on that post.
Prior to increment, the way we would handle this is to create a database transaction action where we can read the current value, increment it, and then update the database with the new value.
This works perfectly fine, but we can now dramatically simplify the process by just passing an increment value to Firestore, and it will do all the calculation for us atomically on the back end.
You can see in the demo here, I have an increment and decrement button.
And when I push these buttons, they either increase or decrease increase the count by 1.
And what makes this magical is that we don't need to know the actual value of the current count to make this update.
Let's take a look at how easy this actually is in the code.
The first thing we'll do is make a reference app to Firestore.
And you wanna make sure you're using the latest version of the SDK because this is a new feature.
And that will give you access to field value increment, app, which is a function that expects an argument with the value that you want to increment by.
For a lot of simple use cases, you can increase your counter by 1 or you can decrease it by passing negative one.
App.
From there, we'll just make a reference to a document in the database, and then we can call update or set with the merge true option to update the counter.
App.
So when you pass the increment field value to Firestore, it will look at the value that currently exists on that document, then perform the math to update it server side, which means that your clients can never update it with the wrong value.
App.
That's how it looks in the code, but you're not limited to just using integer values.
For example, you can pass in floating point or decimal values, and Firestore knows how to perform those calculations as well.
App.
But one of the places I find it most useful is when it's combined with other document rights in multiple locations because it makes it possible to perform data aggregation entirely from the front end without using a cloud function.
One of the most common requirements is to get a count of the total number of documents in a collection.
Now we could just read the collection every time we need the count, app.
But that doesn't scale very well as far as your costs are concerned because you're charged per read, and you'd be sending a ton of unnecessary data over the network.
For this demo, I've created a special document called stats, and its job is to keep the total count of the documents in this collection.
So every time we click this button, we're performing 2 rights.
We're updating the count on the stats document and then we're creating a new document in this collection.
But we only want to update the count if we're 100% sure that that new document has been added to the collection.
And we can easily perform an atomic write on multiple documents by setting them up as a Firestore batch.
And the code will set up a reference to our stats document and then also a reference to a random document ID.
From there, we can set up a batch, which gives us a way to perform multiple rights simultaneously that will all either succeed or fail together.
App.
When dealing with counts, it's very important that they're performed atomically because if the new document is not actually created but we still update the count, then we're gonna be left with a count that's not accurate.
App.
After you define a batch, you can define multiple updates on it.
The first argument is the reference to the document, and the second argument is the data you wanna update.
App.
When updating the stats document, we'll wanna make sure to also pass in the merge true option, which will make it a non destructive update.
If we leave this option out, it will just reset the count every time we run this batch.
App.
And lastly, we call batch commit to tell Firestore to run these updates.
As you can see, increment is a very simple tool, but one that can help prevent a lot of unnecessary complexity in your code.
I'm gonna go ahead and wrap things up there.
If you wanna see more quick videos like this, let me know in the comments.
Thanks for watching, and I will talk to you soon.
App.

Speaker 0: One of the challenges with Firestore is keeping an accurate count on a document because you might have multiple clients competing to update the same value at the same time. Firestore has always had a way to maintain accurate counters in the form of database transactions, but app, but they require a pretty significant amount of code just to do something simple like update a counter. Fortunately, just a couple weeks ago Firestore released a new tool called Increment. App. In today's quick video, you'll learn how increment can help simplify your code and do things like data aggregation entirely from the front end.

If you're new here, like and subscribe, and you can grab the full source code from Fireship I IO. Imagine you're in a situation where you have a blog post and you wanna keep track of the total number of likes on that post. Prior to increment, the way we would handle this is to create a database transaction action where we can read the current value, increment it, and then update the database with the new value. This works perfectly fine, but we can now dramatically simplify the process by just passing an increment value to Firestore, and it will do all the calculation for us atomically on the back end. You can see in the demo here, I have an increment and decrement button.

And when I push these buttons, they either increase or decrease increase the count by 1. And what makes this magical is that we don't need to know the actual value of the current count to make this update. Let's take a look at how easy this actually is in the code. The first thing we'll do is make a reference app to Firestore. And you wanna make sure you're using the latest version of the SDK because this is a new feature.

And that will give you access to field value increment, app, which is a function that expects an argument with the value that you want to increment by. For a lot of simple use cases, you can increase your counter by 1 or you can decrease it by passing negative one. App. From there, we'll just make a reference to a document in the database, and then we can call update or set with the merge true option to update the counter. App.

So when you pass the increment field value to Firestore, it will look at the value that currently exists on that document, then perform the math to update it server side, which means that your clients can never update it with the wrong value. App. That's how it looks in the code, but you're not limited to just using integer values. For example, you can pass in floating point or decimal values, and Firestore knows how to perform those calculations as well. App.

But one of the places I find it most useful is when it's combined with other document rights in multiple locations because it makes it possible to perform data aggregation entirely from the front end without using a cloud function. One of the most common requirements is to get a count of the total number of documents in a collection. Now we could just read the collection every time we need the count, app. But that doesn't scale very well as far as your costs are concerned because you're charged per read, and you'd be sending a ton of unnecessary data over the network. For this demo, I've created a special document called stats, and its job is to keep the total count of the documents in this collection.

So every time we click this button, we're performing 2 rights. We're updating the count on the stats document and then we're creating a new document in this collection. But we only want to update the count if we're 100% sure that that new document has been added to the collection. And we can easily perform an atomic write on multiple documents by setting them up as a Firestore batch. And the code will set up a reference to our stats document and then also a reference to a random document ID.

From there, we can set up a batch, which gives us a way to perform multiple rights simultaneously that will all either succeed or fail together. App. When dealing with counts, it's very important that they're performed atomically because if the new document is not actually created but we still update the count, then we're gonna be left with a count that's not accurate. App. After you define a batch, you can define multiple updates on it.

The first argument is the reference to the document, and the second argument is the data you wanna update. App. When updating the stats document, we'll wanna make sure to also pass in the merge true option, which will make it a non destructive update. If we leave this option out, it will just reset the count every time we run this batch. App.

And lastly, we call batch commit to tell Firestore to run these updates. As you can see, increment is a very simple tool, but one that can help prevent a lot of unnecessary complexity in your code. I'm gonna go ahead and wrap things up there. If you wanna see more quick videos like this, let me know in the comments. Thanks for watching, and I will talk to you soon.

App.
One of the challenges with Firestore is keeping an accurate count on a document because you might have multiple clients competing to update the same value at the same time. Firestore has always had a way to maintain accurate counters in the form of database transactions, but app, but they require a pretty significant amount of code just to do something simple like update a counter.
databases
Fortunately, just a couple weeks ago Firestore released a new tool called Increment. App. In today's quick video, you'll learn how increment can help simplify your code and do things like data aggregation entirely from the front end.
If you're new here, like and subscribe, and you can grab the full source code from Fireship I IO. Imagine you're in a situation where you have a blog post and you wanna keep track of the total number of likes on that post. Prior to increment, the way we would handle this is to create a database transaction action where we can read the current value, increment it, and then update the database with the new value. This works perfectly fine, but we can now dramatically simplify the process by just passing an increment value to Firestore, and it will do all the calculation for us atomically on the back end.
blogging
databases
programming
You can see in the demo here, I have an increment and decrement button. And when I push these buttons, they either increase or decrease increase the count by 1. And what makes this magical is that we don't need to know the actual value of the current count to make this update. Let's take a look at how easy this actually is in the code. The first thing we'll do is make a reference app to Firestore. And you wanna make sure you're using the latest version of the SDK because this is a new feature. And that will give you access to field value increment, app, which is a function that expects an argument with the value that you want to increment by.
For a lot of simple use cases, you can increase your counter by 1 or you can decrease it by passing negative one. App. From there, we'll just make a reference to a document in the database, and then we can call update or set with the merge true option to update the counter.
databases
App. So when you pass the increment field value to Firestore, it will look at the value that currently exists on that document, then perform the math to update it server side, which means that your clients can never update it with the wrong value.
App. That's how it looks in the code, but you're not limited to just using integer values. For example, you can pass in floating point or decimal values, and Firestore knows how to perform those calculations as well. App. But one of the places I find it most useful is when it's combined with other document rights in multiple locations because it makes it possible to perform data aggregation entirely from the front end without using a cloud function. One of the most common requirements is to get a count of the total number of documents in a collection.
databases
data management
data
Now we could just read the collection every time we need the count, app.
But that doesn't scale very well as far as your costs are concerned because you're charged per read, and you'd be sending a ton of unnecessary data over the network. For this demo, I've created a special document called stats, and its job is to keep the total count of the documents in this collection. So every time we click this button, we're performing 2 rights. We're updating the count on the stats document and then we're creating a new document in this collection. But we only want to update the count if we're 100% sure that that new document has been added to the collection.
statistics
data
data management
data analysis
And we can easily perform an atomic write on multiple documents by setting them up as a Firestore batch.
And the code will set up a reference to our stats document and then also a reference to a random document ID. From there, we can set up a batch, which gives us a way to perform multiple rights simultaneously that will all either succeed or fail together. App. When dealing with counts, it's very important that they're performed atomically because if the new document is not actually created but we still update the count, then we're gonna be left with a count that's not accurate. App. After you define a batch, you can define multiple updates on it.
data
The first argument is the reference to the document, and the second argument is the data you wanna update. App. When updating the stats document, we'll wanna make sure to also pass in the merge true option, which will make it a non destructive update. If we leave this option out, it will just reset the count every time we run this batch. App. And lastly, we call batch commit to tell Firestore to run these updates.
As you can see, increment is a very simple tool, but one that can help prevent a lot of unnecessary complexity in your code. I'm gonna go ahead and wrap things up there. If you wanna see more quick videos like this, let me know in the comments. Thanks for watching, and I will talk to you soon. App.
software
software development
microsoft
apple
youtube
programming
One of the challenges with Firestore is keeping an accurate count on a document because you might have multiple clients competing to update the same value at the same time. Firestore has always had a way to maintain accurate counters in the form of database transactions, but app, but they require a pretty significant amount of code just to do something simple like update a counter. Fortunately, just a couple weeks ago Firestore released a new tool called Increment. App. In today's quick video, you'll learn how increment can help simplify your code and do things like data aggregation entirely from the front end. If you're new here, like and subscribe, and you can grab the full source code from Fireship I IO. Imagine you're in a situation where you have a blog post and you wanna keep track of the total number of likes on that post. Prior to increment, the way we would handle this is to create a database transaction action where we can read the current value, increment it, and then update the database with the new value. This works perfectly fine, but we can now dramatically simplify the process by just passing an increment value to Firestore, and it will do all the calculation for us atomically on the back end. You can see in the demo here, I have an increment and decrement button. And when I push these buttons, they either increase or decrease increase the count by 1. And what makes this magical is that we don't need to know the actual value of the current count to make this update. Let's take a look at how easy this actually is in the code. The first thing we'll do is make a reference app to Firestore. And you wanna make sure you're using the latest version of the SDK because this is a new feature. And that will give you access to field value increment, app, which is a function that expects an argument with the value that you want to increment by. For a lot of simple use cases, you can increase your counter by 1 or you can decrease it by passing negative one. App. From there, we'll just make a reference to a document in the database, and then we can call update or set with the merge true option to update the counter. App. So when you pass the increment field value to Firestore, it will look at the value that currently exists on that document, then perform the math to update it server side, which means that your clients can never update it with the wrong value. App. That's how it looks in the code, but you're not limited to just using integer values. For example, you can pass in floating point or decimal values, and Firestore knows how to perform those calculations as well. App. But one of the places I find it most useful is when it's combined with other document rights in multiple locations because it makes it possible to perform data aggregation entirely from the front end without using a cloud function. One of the most common requirements is to get a count of the total number of documents in a collection. Now we could just read the collection every time we need the count, app. But that doesn't scale very well as far as your costs are concerned because you're charged per read, and you'd be sending a ton of unnecessary data over the network. For this demo, I've created a special document called stats, and its job is to keep the total count of the documents in this collection. So every time we click this button, we're performing 2 rights. We're updating the count on the stats document and then we're creating a new document in this collection. But we only want to update the count if we're 100% sure that that new document has been added to the collection. And we can easily perform an atomic write on multiple documents by setting them up as a Firestore batch. And the code will set up a reference to our stats document and then also a reference to a random document ID. From there, we can set up a batch, which gives us a way to perform multiple rights simultaneously that will all either succeed or fail together. App. When dealing with counts, it's very important that they're performed atomically because if the new document is not actually created but we still update the count, then we're gonna be left with a count that's not accurate. App. After you define a batch, you can define multiple updates on it. The first argument is the reference to the document, and the second argument is the data you wanna update. App. When updating the stats document, we'll wanna make sure to also pass in the merge true option, which will make it a non destructive update. If we leave this option out, it will just reset the count every time we run this batch. App. And lastly, we call batch commit to tell Firestore to run these updates. As you can see, increment is a very simple tool, but one that can help prevent a lot of unnecessary complexity in your code. I'm gonna go ahead and wrap things up there. If you wanna see more quick videos like this, let me know in the comments. Thanks for watching, and I will talk to you soon. App.
One
one
of
of
the
the
challenges
challenges
with
with
Firestore
firestore
is
is
keeping
keeping
an
an
accurate
accurate
count
count
on
on
a
a
document
document
because
because
you
you
might
might
have
have
multiple
multiple
clients
clients
competing
competing
to
to
update
update
the
the
same
same
value
value
at
at
the
the
same
same
time.
time
Firestore
firestore
has
has
always
always
had
had
a
a
way
way
to
to
maintain
maintain
accurate
accurate
counters
counters
in
in
the
the
form
form
of
of
database
database
transactions,
transactions
but
but
app,
app
but
but
they
they
require
require
a
a
pretty
pretty
significant
significant
amount
amount
of
of
code
code
just
just
to
to
do
do
something
something
simple
simple
like
like
update
update
a
a
counter.
counter
Fortunately,
fortunately
just
just
a
a
couple
couple
weeks
weeks
ago
ago
Firestore
firestore
released
released
a
a
new
new
tool
tool
called
called
Increment.
increment
App.
app
In
in
today's
today's
quick
quick
video,
video
you'll
you'll
learn
learn
how
how
increment
increment
can
can
help
help
simplify
simplify
your
your
code
code
and
and
do
do
things
things
like
like
data
data
aggregation
aggregation
entirely
entirely
from
from
the
the
front
front
end.
end
If
if
you're
you're
new
new
here,
here
like
like
and
and
subscribe,
subscribe
and
and
you
you
can
can
grab
grab
the
the
full
full
source
source
code
code
from
from
Fireship
fireship
I
i
IO.
io
Imagine
imagine
you're
you're
in
in
a
a
situation
situation
where
where
you
you
have
have
a
a
blog
blog
post
post
and
and
you
you
wanna
wanna
keep
keep
track
track
of
of
the
the
total
total
number
number
of
of
likes
likes
on
on
that
that
post.
post
Prior
prior
to
to
increment,
increment
the
the
way
way
we
we
would
would
handle
handle
this
this
is
is
to
to
create
create
a
a
database
database
transaction
transaction
action
action
where
where
we
we
can
can
read
read
the
the
current
current
value,
value
increment
increment
it,
it
and
and
then
then
update
update
the
the
database
database
with
with
the
the
new
new
value.
value
This
this
works
works
perfectly
perfectly
fine,
fine
but
but
we
we
can
can
now
now
dramatically
dramatically
simplify
simplify
the
the
process
process
by
by
just
just
passing
passing
an
an
increment
increment
value
value
to
to
Firestore,
firestore
and
and
it
it
will
will
do
do
all
all
the
the
calculation
calculation
for
for
us
us
atomically
atomically
on
on
the
the
back
back
end.
end
You
you
can
can
see
see
in
in
the
the
demo
demo
here,
here
I
i
have
have
an
an
increment
increment
and
and
decrement
decrement
button.
button
And
and
when
when
I
i
push
push
these
these
buttons,
buttons
they
they
either
either
increase
increase
or
or
decrease
decrease
increase
increase
the
the
count
count
by
by
1.
1
And
and
what
what
makes
makes
this
this
magical
magical
is
is
that
that
we
we
don't
don't
need
need
to
to
know
know
the
the
actual
actual
value
value
of
of
the
the
current
current
count
count
to
to
make
make
this
this
update.
update
Let's
let's
take
take
a
a
look
look
at
at
how
how
easy
easy
this
this
actually
actually
is
is
in
in
the
the
code.
code
The
the
first
first
thing
thing
we'll
we'll
do
do
is
is
make
make
a
a
reference
reference
app
app
to
to
Firestore.
firestore
And
and
you
you
wanna
wanna
make
make
sure
sure
you're
you're
using
using
the
the
latest
latest
version
version
of
of
the
the
SDK
sdk
because
because
this
this
is
is
a
a
new
new
feature.
feature
And
and
that
that
will
will
give
give
you
you
access
access
to
to
field
field
value
value
increment,
increment
app,
app
which
which
is
is
a
a
function
function
that
that
expects
expects
an
an
argument
argument
with
with
the
the
value
value
that
that
you
you
want
want
to
to
increment
increment
by.
by
For
for
a
a
lot
lot
of
of
simple
simple
use
use
cases,
cases
you
you
can
can
increase
increase
your
your
counter
counter
by
by
1
1
or
or
you
you
can
can
decrease
decrease
it
it
by
by
passing
passing
negative
negative
one.
one
App.
app
From
from
there,
there
we'll
we'll
just
just
make
make
a
a
reference
reference
to
to
a
a
document
document
in
in
the
the
database,
database
and
and
then
then
we
we
can
can
call
call
update
update
or
or
set
set
with
with
the
the
merge
merge
true
true
option
option
to
to
update
update
the
the
counter.
counter
App.
app
So
so
when
when
you
you
pass
pass
the
the
increment
increment
field
field
value
value
to
to
Firestore,
firestore
it
it
will
will
look
look
at
at
the
the
value
value
that
that
currently
currently
exists
exists
on
on
that
that
document,
document
then
then
perform
perform
the
the
math
math
to
to
update
update
it
it
server
server
side,
side
which
which
means
means
that
that
your
your
clients
clients
can
can
never
never
update
update
it
it
with
with
the
the
wrong
wrong
value.
value
App.
app
That's
that's
how
how
it
it
looks
looks
in
in
the
the
code,
code
but
but
you're
you're
not
not
limited
limited
to
to
just
just
using
using
integer
integer
values.
values
For
for
example,
example
you
you
can
can
pass
pass
in
in
floating
floating
point
point
or
or
decimal
decimal
values,
values
and
and
Firestore
firestore
knows
knows
how
how
to
to
perform
perform
those
those
calculations
calculations
as
as
well.
well
App.
app
But
but
one
one
of
of
the
the
places
places
I
i
find
find
it
it
most
most
useful
useful
is
is
when
when
it's
it's
combined
combined
with
with
other
other
document
document
rights
rights
in
in
multiple
multiple
locations
locations
because
because
it
it
makes
makes
it
it
possible
possible
to
to
perform
perform
data
data
aggregation
aggregation
entirely
entirely
from
from
the
the
front
front
end
end
without
without
using
using
a
a
cloud
cloud
function.
function
One
one
of
of
the
the
most
most
common
common
requirements
requirements
is
is
to
to
get
get
a
a
count
count
of
of
the
the
total
total
number
number
of
of
documents
documents
in
in
a
a
collection.
collection
Now
now
we
we
could
could
just
just
read
read
the
the
collection
collection
every
every
time
time
we
we
need
need
the
the
count,
count
app.
app
But
but
that
that
doesn't
doesn't
scale
scale
very
very
well
well
as
as
far
far
as
as
your
your
costs
costs
are
are
concerned
concerned
because
because
you're
you're
charged
charged
per
per
read,
read
and
and
you'd
you'd
be
be
sending
sending
a
a
ton
ton
of
of
unnecessary
unnecessary
data
data
over
over
the
the
network.
network
For
for
this
this
demo,
demo
I've
i've
created
created
a
a
special
special
document
document
called
called
stats,
stats
and
and
its
its
job
job
is
is
to
to
keep
keep
the
the
total
total
count
count
of
of
the
the
documents
documents
in
in
this
this
collection.
collection
So
so
every
every
time
time
we
we
click
click
this
this
button,
button
we're
we're
performing
performing
2
2
rights.
rights
We're
we're
updating
updating
the
the
count
count
on
on
the
the
stats
stats
document
document
and
and
then
then
we're
we're
creating
creating
a
a
new
new
document
document
in
in
this
this
collection.
collection
But
but
we
we
only
only
want
want
to
to
update
update
the
the
count
count
if
if
we're
we're
100%
100%
sure
sure
that
that
that
that
new
new
document
document
has
has
been
been
added
added
to
to
the
the
collection.
collection
And
and
we
we
can
can
easily
easily
perform
perform
an
an
atomic
atomic
write
write
on
on
multiple
multiple
documents
documents
by
by
setting
setting
them
them
up
up
as
as
a
a
Firestore
firestore
batch.
batch
And
and
the
the
code
code
will
will
set
set
up
up
a
a
reference
reference
to
to
our
our
stats
stats
document
document
and
and
then
then
also
also
a
a
reference
reference
to
to
a
a
random
random
document
document
ID.
id
From
from
there,
there
we
we
can
can
set
set
up
up
a
a
batch,
batch
which
which
gives
gives
us
us
a
a
way
way
to
to
perform
perform
multiple
multiple
rights
rights
simultaneously
simultaneously
that
that
will
will
all
all
either
either
succeed
succeed
or
or
fail
fail
together.
together
App.
app
When
when
dealing
dealing
with
with
counts,
counts
it's
it's
very
very
important
important
that
that
they're
they're
performed
performed
atomically
atomically
because
because
if
if
the
the
new
new
document
document
is
is
not
not
actually
actually
created
created
but
but
we
we
still
still
update
update
the
the
count,
count
then
then
we're
we're
gonna
gonna
be
be
left
left
with
with
a
a
count
count
that's
that's
not
not
accurate.
accurate
App.
app
After
after
you
you
define
define
a
a
batch,
batch
you
you
can
can
define
define
multiple
multiple
updates
updates
on
on
it.
it
The
the
first
first
argument
argument
is
is
the
the
reference
reference
to
to
the
the
document,
document
and
and
the
the
second
second
argument
argument
is
is
the
the
data
data
you
you
wanna
wanna
update.
update
App.
app
When
when
updating
updating
the
the
stats
stats
document,
document
we'll
we'll
wanna
wanna
make
make
sure
sure
to
to
also
also
pass
pass
in
in
the
the
merge
merge
true
true
option,
option
which
which
will
will
make
make
it
it
a
a
non
non
destructive
destructive
update.
update
If
if
we
we
leave
leave
this
this
option
option
out,
out
it
it
will
will
just
just
reset
reset
the
the
count
count
every
every
time
time
we
we
run
run
this
this
batch.
batch
App.
app
And
and
lastly,
lastly
we
we
call
call
batch
batch
commit
commit
to
to
tell
tell
Firestore
firestore
to
to
run
run
these
these
updates.
updates
As
as
you
you
can
can
see,
see
increment
increment
is
is
a
a
very
very
simple
simple
tool,
tool
but
but
one
one
that
that
can
can
help
help
prevent
prevent
a
a
lot
lot
of
of
unnecessary
unnecessary
complexity
complexity
in
in
your
your
code.
code
I'm
i'm
gonna
gonna
go
go
ahead
ahead
and
and
wrap
wrap
things
things
up
up
there.
there
If
if
you
you
wanna
wanna
see
see
more
more
quick
quick
videos
videos
like
like
this,
this
let
let
me
me
know
know
in
in
the
the
comments.
comments
Thanks
thanks
for
for
watching,
watching
and
and
I
i
will
will
talk
talk
to
to
you
you
soon.
soon
App.
app
success
The speaker explains how to use the new tool Increment in FirAGG to simplify code and update a file. The tool provides a reference app to FirQuad and allows for multiple updates. The caller explains how to set up multiple rights and batch commit to avoid unnecessary complexity in code.
Firestore Increment - Counters that Scale
One of the challenges with Firestore is keeping an accurate count on a document because you might have multiple clients competing to update the same value at the same time. Firestore has always had a way to maintain accurate counters in the form of database transactions, but app, but they require a pretty significant amount of code just to do something simple like update a counter. Fortunately, just a couple weeks ago Firestore released a new tool called Increment. App. In today's quick video, you'll learn how increment can help simplify your code and do things like data aggregation entirely from the front end. If you're new here, like and subscribe, and you can grab the full source code from Fireship I IO. Imagine you're in a situation where you have a blog post and you wanna keep track of the total number of likes on that post. Prior to increment, the way we would handle this is to create a database transaction action where we can read the current value, increment it, and then update the database with the new value. This works perfectly fine, but we can now dramatically simplify the process by just passing an increment value to Firestore, and it will do all the calculation for us atomically on the back end. You can see in the demo here, I have an increment and decrement button. And when I push these buttons, they either increase or decrease increase the count by 1. And what makes this magical is that we don't need to know the actual value of the current count to make this update. Let's take a look at how easy this actually is in the code. The first thing we'll do is make a reference app to Firestore. And you wanna make sure you're using the latest version of the SDK because this is a new feature. And that will give you access to field value increment, app, which is a function that expects an argument with the value that you want to increment by. For a lot of simple use cases, you can increase your counter by 1 or you can decrease it by passing negative one. App. From there, we'll just make a reference to a document in the database, and then we can call update or set with the merge true option to update the counter. App. So when you pass the increment field value to Firestore, it will look at the value that currently exists on that document, then perform the math to update it server side, which means that your clients can never update it with the wrong value. App. That's how it looks in the code, but you're not limited to just using integer values. For example, you can pass in floating point or decimal values, and Firestore knows how to perform those calculations as well. App. But one of the places I find it most useful is when it's combined with other document rights in multiple locations because it makes it possible to perform data aggregation entirely from the front end without using a cloud function. One of the most common requirements is to get a count of the total number of documents in a collection. Now we could just read the collection every time we need the count, app. But that doesn't scale very well as far as your costs are concerned because you're charged per read, and you'd be sending a ton of unnecessary data over the network. For this demo, I've created a special document called stats, and its job is to keep the total count of the documents in this collection. So every time we click this button, we're performing 2 rights. We're updating the count on the stats document and then we're creating a new document in this collection. But we only want to update the count if we're 100% sure that that new document has been added to the collection. And we can easily perform an atomic write on multiple documents by setting them up as a Firestore batch. And the code will set up a reference to our stats document and then also a reference to a random document ID. From there, we can set up a batch, which gives us a way to perform multiple rights simultaneously that will all either succeed or fail together. App. When dealing with counts, it's very important that they're performed atomically because if the new document is not actually created but we still update the count, then we're gonna be left with a count that's not accurate. App. After you define a batch, you can define multiple updates on it. The first argument is the reference to the document, and the second argument is the data you wanna update. App. When updating the stats document, we'll wanna make sure to also pass in the merge true option, which will make it a non destructive update. If we leave this option out, it will just reset the count every time we run this batch. App. And lastly, we call batch commit to tell Firestore to run these updates. As you can see, increment is a very simple tool, but one that can help prevent a lot of unnecessary complexity in your code. I'm gonna go ahead and wrap things up there. If you wanna see more quick videos like this, let me know in the comments. Thanks for watching, and I will talk to you soon. App.
The speaker explains how to use the new tool Increment in FirAGG to simplify code and update a file. The tool provides a reference app to FirQuad and allows for multiple updates. The caller explains how to set up multiple rights and batch commit to avoid unnecessary complexity in code.

        [INST]
        You are youtuber called Fireship you make engaging high-intensity and entertaining coding tutorials and tech news. 
        you covers a wide range of topics relevant to programmers, aiming to help them learn and improve their skills quickly.
        
        Given the title of the video : Firestore Increment - Counters that Scale 
        and a small summary : The speaker explains how to use the new tool Increment in FirAGG to simplify code and update a file. The tool provides a reference app to FirQuad and allows for multiple updates. The caller explains how to set up multiple rights and batch commit to avoid unnecessary complexity in code.
        [/INST]
        
        Generate the video : One of the challenges with Firestore is keeping an accurate count on a document because you might have multiple clients competing to update the same value at the same time. Firestore has always had a way to maintain accurate counters in the form of database transactions, but app, but they require a pretty significant amount of code just to do something simple like update a counter. Fortunately, just a couple weeks ago Firestore released a new tool called Increment. App. In today's quick video, you'll learn how increment can help simplify your code and do things like data aggregation entirely from the front end. If you're new here, like and subscribe, and you can grab the full source code from Fireship I IO. Imagine you're in a situation where you have a blog post and you wanna keep track of the total number of likes on that post. Prior to increment, the way we would handle this is to create a database transaction action where we can read the current value, increment it, and then update the database with the new value. This works perfectly fine, but we can now dramatically simplify the process by just passing an increment value to Firestore, and it will do all the calculation for us atomically on the back end. You can see in the demo here, I have an increment and decrement button. And when I push these buttons, they either increase or decrease increase the count by 1. And what makes this magical is that we don't need to know the actual value of the current count to make this update. Let's take a look at how easy this actually is in the code. The first thing we'll do is make a reference app to Firestore. And you wanna make sure you're using the latest version of the SDK because this is a new feature. And that will give you access to field value increment, app, which is a function that expects an argument with the value that you want to increment by. For a lot of simple use cases, you can increase your counter by 1 or you can decrease it by passing negative one. App. From there, we'll just make a reference to a document in the database, and then we can call update or set with the merge true option to update the counter. App. So when you pass the increment field value to Firestore, it will look at the value that currently exists on that document, then perform the math to update it server side, which means that your clients can never update it with the wrong value. App. That's how it looks in the code, but you're not limited to just using integer values. For example, you can pass in floating point or decimal values, and Firestore knows how to perform those calculations as well. App. But one of the places I find it most useful is when it's combined with other document rights in multiple locations because it makes it possible to perform data aggregation entirely from the front end without using a cloud function. One of the most common requirements is to get a count of the total number of documents in a collection. Now we could just read the collection every time we need the count, app. But that doesn't scale very well as far as your costs are concerned because you're charged per read, and you'd be sending a ton of unnecessary data over the network. For this demo, I've created a special document called stats, and its job is to keep the total count of the documents in this collection. So every time we click this button, we're performing 2 rights. We're updating the count on the stats document and then we're creating a new document in this collection. But we only want to update the count if we're 100% sure that that new document has been added to the collection. And we can easily perform an atomic write on multiple documents by setting them up as a Firestore batch. And the code will set up a reference to our stats document and then also a reference to a random document ID. From there, we can set up a batch, which gives us a way to perform multiple rights simultaneously that will all either succeed or fail together. App. When dealing with counts, it's very important that they're performed atomically because if the new document is not actually created but we still update the count, then we're gonna be left with a count that's not accurate. App. After you define a batch, you can define multiple updates on it. The first argument is the reference to the document, and the second argument is the data you wanna update. App. When updating the stats document, we'll wanna make sure to also pass in the merge true option, which will make it a non destructive update. If we leave this option out, it will just reset the count every time we run this batch. App. And lastly, we call batch commit to tell Firestore to run these updates. As you can see, increment is a very simple tool, but one that can help prevent a lot of unnecessary complexity in your code. I'm gonna go ahead and wrap things up there. If you wanna see more quick videos like this, let me know in the comments. Thanks for watching, and I will talk to you soon. App.
        