0

I have the following Firebase structure

enter image description here

There is a node that keeps a list of albums by user. In certain circumstances I do not have the user id (E7Bv..), I only have the album id (-L0uG...). If I had both then I could easily access the specific album node.

Since I do not have the userid, i need a way to query the node where the albumin is equal to the value I have in hand. I do not see how to structure such a query.

As one approach I also tried to add the albumid (-L0uG...) to the sub node as the value of _key. I'd prefer not to have to duplicate that value and just query for where the sub-node for albumid equals the value I have in hand for albumid.

Or, if that can not be done then can anyone tell me how to query where the sub-node has a value for _key that matches the value i have in hand - Without knowing the userid node value?

I would like to do something like this ... (where albumRef is the top node for byUser_albums)

 albumRef.queryOrdered(byChild: "_key").queryEqual(toValue: albumID).observeSingleEvent(of: .value, with:  { snapshot in

of course this fails because _key is not on the userid node, it is on the albumid node. I need to either query by the subnode id, or go deeper to query by _key

UPDATE

From Frank Below. I tried this ...

  albumRef.queryOrdered(byChild: albumID).queryStarting(atValue: nil).observeSingleEvent(of: .value, with:  { snapshot in

This works and finds the right data. It returns the key of the scoping autoid. The only downside is that Firebase says (in debug log) ....

"**Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "-L0vTQtLwBe_hilTOGid" at /byUser_albums to your security rules for better performance**" 

If I were doing a typical read then I could just add the rule to the database, but I can't add this rule because the value that it wants the .indexOn is not a static value - it's an auto. I don't see a way to index on the autoid. I think I will have to restructure the albums node to have user as an attribute instead of defining a scoping node by user. I do not see a way to read without downloading all data and filter on the client.

john
  • 505
  • 1
  • 7
  • 15
  • This is what you're looking for: https://stackoverflow.com/questions/40713137/querying-below-autoids-in-firebase – DoesData Dec 21 '17 at 19:21
  • I have a slight difference in that I know the value of the subordinate autoid. I don't need to query by a value of the subordinate autoid - if somehow i could just query where the subordinate autoid is equal to the value i have in hand. if "Firebase Database queries properties one level deeper than the location where you run them." then the subordinate autoid IS only one level deep. Can I not query where the subordinate autoid matches? Maybe not allowed :( – john Dec 21 '17 at 19:38
  • I think this might work: `albumRef.queryOrdered(byChild: albumID).queryStarting(atValue: nil)` – Frank van Puffelen Dec 21 '17 at 21:52
  • Thanks. Great to know how to do that. It sent me to yet another block - where I can't set a .indexOn value because the value is dynamic. See update note on main body. – john Dec 22 '17 at 00:00

0 Answers0