1

I'm using Firebase Cloud Firestore and can't figure out how to access documents without knowing the specific path. The database structure is users/{user id}/favourites/{favourite id}. There are no fields in users/{user id} only subcollections. Knowing the user id, i can get the favourites for the user, but I can't get a list of users to get everyone's favourites. Here is the code I am trying (Java admin SDK):

db.collection("users").get().get()

which results in an empty Iterable with no DocumentSnapshots.

How can I get a list of the most popular favourites?

EDIT: I've discovered I can get a list of users with no fields if I add a field. Even if I delete it later, it still appears as a document in the collection.

EDIT2: I've discovered that I can create an empty document, so I'm just doing that for now. As a one-off, I can get a list of all users from firebase auth and look up which ones have a favourites collection and just set those to empty documents.

Jayen
  • 5,653
  • 2
  • 44
  • 65
  • You need to know the path. There's no way to get there without knowing. – Doug Stevenson Jan 30 '18 at 04:21
  • What you're trying to do is read/query across the sub-collections of multiple documents. As Doug says, this is not possible at the moment. See https://stackoverflow.com/a/46573167/209103 – Frank van Puffelen Jan 30 '18 at 05:06
  • @FrankvanPuffelen that's not actually what I'm trying to do. I know I can't do that so I'm just trying to get a list of users. I have found that I can add [and remove!] a dummy field and it works. I've edited my question. Please consider unmarking this as a duplicate. – Jayen Jan 30 '18 at 05:43
  • If you're never going to have any meaningful data in user documents, then perhaps you should revise your schema. How about just `favorites/{fav_id}` and save `user_id` as an additional field on the leaf documents? You can get favorites of a user by `favorites.whereEqualTo('user_id', 'bob')`. – Hiranya Jayathilaka Jan 30 '18 at 20:26
  • @HiranyaJayathilaka thanks for your suggestion. I understand the write rate to the same document is quite limited. That's why i have chosen to use separate documents. – Jayen Jan 30 '18 at 23:38
  • @HiranyaJayathilaka thanks again for your suggestion. what i will do is have a collection `userFavourites` with automatic ids and contents as `{user: 'user', favourite: 'favourite'}` – Jayen Jan 31 '18 at 07:17
  • @DougStevenson The Firebase Console somehow knows about subcollections, even with no parent document. – DarkNeuron May 24 '18 at 12:56
  • @DarkNeuron I had asked https://stackoverflow.com/users/5081766/bojeil (Firebase staff) about this somewhere, and even he doesn't know how the console does it. Probably not something exposed by the Firebase public API. – Jayen May 24 '18 at 22:37
  • Yeah I've concluded the same, its a private API sadly. – DarkNeuron May 25 '18 at 07:04
  • Thanks very much for this - went down a 2 hour hole trying to figure out why no documents were returned. This technique worked brilliantly. :-) – Elroid Mar 21 '19 at 15:13

0 Answers0