Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore: Query by item in array of document

I have 2 collections "photos" and "users" and each document in "users" has one or more photo IDs with an array.

photos > 5528c46b > name: "Photo1"          a1e820eb > name: "Photo2"          32d410a7 > name: "Photo3" 
users > acd02b1d > name: "John", photos: ["5528c46b"]         67f60ad3 > name: "Tom", photos: ["5528c46b", "32d410a7"]         7332ec75 > name: "Sara", photos: ["a1e820eb"]         9f4edcc1 > name: "Anna", photos: ["32d410a7"] 

I want to get all users who have one or more specific photo IDs.

Are there any ways to do that?

like image 985
Jaap Weijland Avatar asked Oct 20 '17 12:10

Jaap Weijland


1 Answers

See Henry's answer, as we've no made Array Contains queries available.

Unfortunately not yet, although it's on our roadmap.

In the meantime, you'll need to use a map instead, in the form of:

photos: {     id1: true     id2: true } 

Now you can find all users with id1 by filtering by photos.id1 == true.

Read more about querying such sets in the Firebase documentation.

like image 182
Dan McGrath Avatar answered Sep 22 '22 19:09

Dan McGrath