1

I am playing around with the firebase sample code of realtime database https://github.com/firebase/quickstart-android/tree/master/database

I am new to JSON and i tried many method and cannot figure out a query that return post starred by user B (so that user B can see the post he liked before) Or do I need to build another tree?

Query I tried:

public Query getQuery(DatabaseReference databaseReference) {
    // All my posts

    return databaseReference.child("posts").child("stars").child(getUid()).equalTo(true);

}

My JSON tree:

{  
   "posts":{  
      "-KMPiC6Okoe2dd35keT6":{  
         "author":"user A",
         "body":"post by user A",
         "starCount":1,
         "stars":{  
            "user B":true
         },
         "timeStamp":"1468253393509",
         "title":"post by user1",
         "uid":"user A",
         "viewCount":0
      },
      "-KMPiIHQXrZIfnv2uNV-":{  
         "author":"user B",
         "body":"post by user B",
         "starCount":0,
         "timeStamp":"1468253419640",
         "title":"post by user B",
         "uid":"user B",
         "viewCount":0
      }
   },
   "user-posts":{  
      "user A":{  
         "-KMPiC6Okoe2dd35keT6":{  
            "author":"user A",
            "body":"post by user A",
            "starCount":1,
            "stars":{  
               "user B":true
            },
            "timeStamp":"1468253393509",
            "title":"post by user A",
            "uid":"user A",
            "viewCount":0
         }
      },
      "user B":{  
         "-KMPiIHQXrZIfnv2uNV-":{  
            "author":"lui",
            "body":"post by user 2",
            "starCount":0,
            "timeStamp":"1468253419640",
            "title":"post by user 2",
            "uid":"user B",
            "viewCount":0
         }
      }
   },
   "users":{  
      "user A":{  
         "email":"userA@gmail.com",
         "username":"user A"
      },
      "user B":{  
         "email":"userB@gmail.com",
         "username":"user B"
      }
   }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Simon Ho
  • 588
  • 1
  • 8
  • 21

2 Answers2

3

First, you are missing a reference:

// Old
databaseReference.child("posts").child("stars").child(getUid()).equalTo(true);

// New
databaseReference.child("posts").child(some-post).child("stars").child(getUid()).equalTo(true);

But still this would be very difficult or even impossible to do that search straight up, without needing to sift through the data returned.

What I advise to do is create a users tree and write the starred posts to the specific user instead of the users to the posts. e.g.:

"users": {
    "userA": {
        "starred": {
            "-KMP3242nf23nfn23": {
                "author": "userB",
                "post": "-KMPiC6Okoe2dd35keT6"
            },
            "-KMPiIHQXrZIfnv2uNV-": {
                "author": "userB",
                "post": "-KMPiC6Okoe2dd35keT6"
            },
            ...
        }
    }
}

(push the new starred posts onto starred)

Then you could make a query as such:

// Get a list of posts starred
... databaseReference.child("users").child(getUid()).child(starred);
// Iterate through and get value of `author` and `post`
...
// Store the values in variables
var author = "...";
var post = "...";

// Make the call
return databaseReference.child("posts").child(post);

Other reference: Firebase - How do I write multiple orderByChild for extracting data?

Comment with any questions.

Community
  • 1
  • 1
theblindprophet
  • 7,767
  • 5
  • 37
  • 55
1

This is pretty straight forward deep query

Here's a Swift solution (using v2.x Firebase) since the platform was not specified.

    let ref = self.myRootRef.childByAppendingPath("posts")

    ref.queryOrderedByChild("stars/user B").queryEqualToValue(true)
           .observeEventType(.Value, withBlock: { snapshot in

        if ( snapshot.value is NSNull ) {
            print("not found")
        } else {
            for child in snapshot.children {

                let key = child.key as String
                let author = child.value["author"] as! String
                print("key = \(key)  author = \(author)")

            }
        }
    })

this will print

key = -KMPiC6Okoe2dd35keT6  author = user A

and if there are other nodes that also match the criteria, they will print as well.

Jay
  • 34,438
  • 18
  • 52
  • 81