Reputation: 2962
I have the following database structure:
Firestore-root
|
--- lists (collection)
|
--- userId1 (document)
| |
| --- userLists (collection)
| |
| --- docKey1 (document)
| | |
| | --- listName: "ListOne"
| | |
| | --- date: January 15, 2018 at 9:56:24 AM UTC+2
| |
| --- docKey2 (document)
| |
| --- listName: "ListTwo"
| |
| --- date: January 15, 2018 at 9:58:12 AM UTC+2
|
--- userId2 (document)
|
--- userLists (collection)
|
--- docKey1 (document)
| |
| --- listName: "ListOne"
| |
| --- date: January 15, 2018 at 9:56:24 AM UTC+2
|
--- docKey3 (document)
|
--- listName: "ListThree"
|
--- date: January 15, 2018 at 9:59:47 AM UTC+2
Using this database structure, I can simply query all lists that correspond to a specific user. But, how can I query the database to get all the users that have a specific list?
In this particular case, how to get all the users that have ListOne
as a list? The result should be: userId1
Do I need to duplicate data or is there another way for doing this?
Edit:
Firestore-root
|
--- allList
|
--- listId1 (collection)
|
--- userId1 (document)
| |
| --- username: "FirstUser"
| |
| --- //other details
|
--- userId2 (document)
|
--- username: "SecondUser"
|
--- //other details
Thanks!
Upvotes: 1
Views: 543
Reputation: 138824
There actually a trick that can be done. Instead of duplicating data, you can add in your docKey1
document a map
, as Todd Kerpelman said here. Your document whould look like this:
userLists (collection)
|
--- docKey1 (document)
|
--- listName: "ListOne"
|
--- date: January 15, 2018 at 9:56:24 AM UTC+2
|
--- users
|
--- userId1: true
|
--- userId2: true
So you can query that particular document to get all the user ids. So in this way you can get a sort what you are asking for.
Upvotes: 0
Reputation: 317467
There's no way to extend a query across multiple subcollections. A query must be contained within a single collection or subcollection. So, you're correct in guessing that you'll need to duplicate data in order to perform your query.
Upvotes: 2