1

I am using Firebase database to store data. I am trying to snapshot every instance that a specific uid appears within a list. I am having trouble querying these ids.

CODE:

func reviews() {

    let ref = Database.database().reference()

    let user = Auth.auth().currentUser
    let uid = user?.uid

    ref.child("reviews").queryEqual(toValue: uid!).observe(DataEventType.childAdded, with: { (info) in

            print(info) //prints null

    })

}

Database:

firebase database

What is the proper way of querying to show every instance of the uid (f7w0q6....)

JSharpp
  • 459
  • 1
  • 9
  • 21

1 Answers1

2

If you're able to change the NoSQL modeling:

I would suggest to create an additional child, like user_review with the structure:

userUID
    reviewID
    reviewID

userUID
    reviewID

...

Then you would query on that userUID and retrieve the values you want.

JSharpp
  • 459
  • 1
  • 9
  • 21
Gustavo Vollbrecht
  • 3,188
  • 2
  • 19
  • 37
  • I have it set up this way because there are other times that I query the filmID. So either way I will have to query this way. I have just decided to structure it this way for personal preference. I basically need a way to search through every filmID for instances of the specific userID. – JSharpp Dec 12 '18 at 02:49
  • By using denormalisation in NoSQL databases, you're able to store all the data required to process a query in one place. Otherwise you would have to query on `reviews` and iterate inside every element to find matching values. – Gustavo Vollbrecht Dec 12 '18 at 02:53
  • Oh, so those numbers are filmIDs? So by reviewID I meant filmID – Gustavo Vollbrecht Dec 12 '18 at 02:58
  • Yes. Each time a review is posted for a film its filmID is posted then under that the usersID is posted. I originally had it set up as you have posted but its been working smoother this way. Until now that I have to find each time a specific userID is posted. I switched it because I ran into this same problem just reversed. – JSharpp Dec 12 '18 at 03:00
  • take a look at (7) Index Table – Gustavo Vollbrecht Dec 12 '18 at 03:01
  • 1
    It sounds like you have a many-to-many problem: you want to find the reviews for a film, but also the film for a review. Adding additional data structures is typically the solution. Also see https://stackoverflow.com/questions/41527058/many-to-many-relationship-in-firebase – Frank van Puffelen Dec 12 '18 at 03:20
  • So adding an additional data structure is the only solution? Your link is helpful as that is similar to my situation. – JSharpp Dec 12 '18 at 21:30
  • One solution is adding additional data structure, another solution is querying on parent and iterating through every node for matching values. Surely the first one is more efficient. – Gustavo Vollbrecht Dec 13 '18 at 12:15