0

I want to query data that is two levels down, however, would I still be able to retrieve data from its original node?

To explain better, my Firebase Database looks like:

posts
  -192u3jdj0j9sj0
     -message: haha this is funny (CAN I STILL GET THIS DATA)
     -genre: comedy (CAN I STILL GET THIS DATA)
        -author
           -user: "jasonj"
        -comment
           -ajiwj2319j0jsf9d0jf
               -comment: "lol"
               -user: "David" (QUERY HERE****)
           -jfaiwjfoj1ijifjojif
               -comment: "so funny"
               -user: "Toddy"

I essentially want to query by all of the comments David has posted. However, with how query works, can I still grab the original (message & genre) that was from "level 1"? Or would I have to restructure my data? Possibly rewriting the level 1 data under comment.

(End goal: something like Yahoo answers, where the user can see the questions he posted, as well as the questions to where he posted comments)

Below code works, but I'm not sure how to pull up level 1 data or if its even possible

   ref = Database.database().reference().child("posts").child(myPost).child("comment")
    var queryRef:DatabaseQuery
    queryRef = ref.queryOrdered(byChild: "user").queryEqual(toValue: "David")
    queryRef.observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.childrenCount > 0 {

1 Answers1

0

Your current data structure makes it easy to find the comments for a specific post. It does not however make it easy to find the comments from a specific author. The reason for that is that Firebase Database queries treat your content as a flat list of nodes. The value you want to filter on, must be at a fixed path under each node.

To allow finding the comments from a specific author, you'll want to add an additional node where you keep that information. For example:

"authorComments": {
  "David": {
    "-192u3jdj0j9sj0_-ajiwj2319j0jsf9d0jf": true
  },
  "Toddy": {
    "-192u3jdj0j9sj0_-jfaiwjfoj1ijifjojif": true
  }
}

This structure is often known as a reverse index, and it allows you to easily find the comment paths (I used a _ as the separator of path segments above) for a specific user.

This sort of data duplication is quite common when using NoSQL databases, as you often have to modify/expand your data structure to allow the use-cases that your app needs.

Also see my answers here:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807