Fireship · @fireship

Firestore Array Query

Uploaded August 3, 2018 · Length 3:38

Firestore Array Query

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.

  1. 0:00

    [Music]

  2. 0:06

    version 5.3 of the firebase web SDK came

  3. 0:10

    out a couple weeks ago and it includes a

  4. 0:12

    cool new feature to query documents

  5. 0:14

    based on an array value this means you

  6. 0:16

    can get all of the documents that have

  7. 0:18

    an array filled that contain a certain

  8. 0:19

    value in that array this video is going

  9. 0:22

    to be a lot shorter than most the

  10. 0:23

    content you'll find on my channel but if

  11. 0:25

    you like this format please leave me a

  12. 0:26

    comment below because I'm considering

  13. 0:28

    adding a couple extra videos to the

  14. 0:30

    channel per week that are just a couple

  15. 0:32

    minutes long that cover really focused

  16. 0:33

    topics like this one here alright so

  17. 0:36

    let's just go ahead and jump right into

  18. 0:37

    the code I'm here in an empty directory

  19. 0:39

    and I have firebase tools installed and

  20. 0:42

    I'll initialize a new project by running

  21. 0:43

    firebase in it and then we'll set up

  22. 0:46

    fire store as well as hosting and if you

  23. 0:49

    already have a firebase project you can

  24. 0:51

    just skip this step completely but make

  25. 0:53

    sure you have version 5.3 or greater of

  26. 0:55

    the SDK installed at this point I can

  27. 0:58

    just run firebase serve in my project

  28. 1:00

    and we should get this initial splash

  29. 1:01

    page the next thing I'll do is take out

  30. 1:05

    the default JavaScript that's provided

  31. 1:07

    by firebase and we'll just replace this

  32. 1:08

    with our own app.js file now that we

  33. 1:12

    have our web app setup we can start

  34. 1:13

    writing the actual code the first thing

  35. 1:15

    we want to do is make a reference to

  36. 1:17

    firestorm then we also need to update

  37. 1:19

    our fire store settings to avoid an

  38. 1:21

    error in the console which is just

  39. 1:23

    setting the timestamps and snapshots to

  40. 1:25

    true then I have some existing data in

  41. 1:27

    my database which is in the carts

  42. 1:29

    collection you can imagine this as a

  43. 1:32

    shopping cart in an e-commerce

  44. 1:33

    application but let's jump to the

  45. 1:35

    firebase console to see what it looks

  46. 1:37

    like each cart has an items field which

  47. 1:40

    is an array of different string values

  48. 1:43

    so right now you can see that carts a

  49. 1:45

    and B both have bananas in them let's go

  50. 1:48

    ahead and add a third cart that also

  51. 1:50

    contains bananas but some additional

  52. 1:52

    values as well and our goal is to be

  53. 1:54

    able to query these documents based on

  54. 1:56

    the shared values in this items array to