Firestore: Query by item in array of document

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Firestore: Query by item in array of document



I have a document "user" which has a key called "photos". Here is an array of strings being saved, namely the IDs of the "photo" documents.



I want to query "user" by this "photos" field: I want all users who have this ID in the "photos" property.



Is this possible through firestore?




3 Answers
3



Added 'array-contains' query operator for use with .where() to find documents where an array field contains a specific element.



https://firebase.google.com/support/release-notes/js 5.3.0



Update: also available in @google-cloud/firestore: https://github.com/googleapis/nodejs-firestore/releases/tag/v0.16.0


@google-cloud/firestore



Update 2 https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html



Update 3 now available in Admin Node.js SDK v6.0.0 https://github.com/firebase/firebase-admin-node/releases





This is great. Thanks for the headsup.
– Jaap Weijland
Jul 25 at 15:03





Welp that took a while :D I was working on a project where I needed this and I'm glad that I lagged back enough for the firebase team to get in the lead :p
– madprogramer
Aug 3 at 19:34





It is Awesome! +1
– Bohdan Didukh
Aug 14 at 11:39





Unfortunately, there are no ways to make a query with few 'array-contains' values.
– Bohdan Didukh
Aug 14 at 12:05





@Henry, yes, I tried it. Firebase server responds me with error: FirebaseError: Invalid query. Queries only support a single array-contains filter.
– Bohdan Didukh
Aug 22 at 11:03



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.


photos.id1 == true



Read more about querying such sets in the Firebase documentation.





Is this roadmap published somewhere? Just curious.
– Jaap Weijland
Oct 21 '17 at 21:31





@DanMcGrath Is there any update to this Feature / Roadmap?
– Gal Bracha
Nov 5 '17 at 13:23





Firestore won't let me query data this way without indexing every single possible id. Is there some way to not require an index, or to have a dynamic index?
– Man Personson
Dec 24 '17 at 21:10





I'm thinking Dan is saying don't use an array field to store the id's, use an object, or a "map". Seems like no big deal/headache to me. Is there any downside to using the map/object vs array? I can't think of any... just my 2 cents.
– Ron Royston
Mar 10 at 22:23





The biggest issue as mentioned by @KevinBeal is that you can index the keys if you need a compound query. This is a pretty big deal breaker for any advanced query functionality.
– aglassman
May 22 at 16:50




Here is a bit of expansion on the answer as some seem to be confused about having to make indexes for each key, Firestore already indexes your data for simple queries thus you can do a simple query like


documentReference.where('param','==','value').onSnapshot(...)



but you can not do a compound query unless you index your data for those parameters. So you would need indexes to be able to do something like this:


documentReference.where('param','==','value').where(..otherparams...).onSnapshot(...)



So as long as you need the photos for an id you can save them as


usersCollection : (a collection)
uidA: (a document)
photoField: (a field value that is a map or object)
fieldID1 : true (a property of the photoField)
fieldID2 : true (a property of the photoField)
etc ...



and you can simply query user(s) that have, let's say, fieldID1 in their photoField without needing to form any index and like query below.


firestore.doc('usersCollection/uidA').where('photoField.fieldID1','==',true).onSnapshot(...)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard