Fireship · @fireship
Firestore Array Query
Uploaded August 3, 2018 · Length 3:38
Summary
how to query firestore documents based on array values using the 'array-contains' operator
- firestore array query
- firebase sdk 5.3
- array-contains operator
- firebase firestore query
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
[Music]
- 0:06
version 5.3 of the firebase web SDK came
- 0:10
out a couple weeks ago and it includes a
- 0:12
cool new feature to query documents
- 0:14
based on an array value this means you
- 0:16
can get all of the documents that have
- 0:18
an array filled that contain a certain
- 0:19
value in that array this video is going
- 0:22
to be a lot shorter than most the
- 0:23
content you'll find on my channel but if
- 0:25
you like this format please leave me a
- 0:26
comment below because I'm considering
- 0:28
adding a couple extra videos to the
- 0:30
channel per week that are just a couple
- 0:32
minutes long that cover really focused
- 0:33
topics like this one here alright so
- 0:36
let's just go ahead and jump right into
- 0:37
the code I'm here in an empty directory
- 0:39
and I have firebase tools installed and
- 0:42
I'll initialize a new project by running
- 0:43
firebase in it and then we'll set up
- 0:46
fire store as well as hosting and if you
- 0:49
already have a firebase project you can
- 0:51
just skip this step completely but make
- 0:53
sure you have version 5.3 or greater of
- 0:55
the SDK installed at this point I can
- 0:58
just run firebase serve in my project
- 1:00
and we should get this initial splash
- 1:01
page the next thing I'll do is take out
- 1:05
the default JavaScript that's provided
- 1:07
by firebase and we'll just replace this
- 1:08
with our own app.js file now that we
- 1:12
have our web app setup we can start
- 1:13
writing the actual code the first thing
- 1:15
we want to do is make a reference to
- 1:17
firestorm then we also need to update
- 1:19
our fire store settings to avoid an
- 1:21
error in the console which is just
- 1:23
setting the timestamps and snapshots to
- 1:25
true then I have some existing data in
- 1:27
my database which is in the carts
- 1:29
collection you can imagine this as a
- 1:32
shopping cart in an e-commerce
- 1:33
application but let's jump to the
- 1:35
firebase console to see what it looks
- 1:37
like each cart has an items field which
- 1:40
is an array of different string values
- 1:43
so right now you can see that carts a
- 1:45
and B both have bananas in them let's go
- 1:48
ahead and add a third cart that also
- 1:50
contains bananas but some additional
- 1:52
values as well and our goal is to be
- 1:54
able to query these documents based on
- 1:56
the shared values in this items array to


