如何查询返回由特定用户主演的帖子?

时间:2016-07-11 16:24:47

标签: json firebase firebase-realtime-database

我正在使用实时数据库https://github.com/firebase/quickstart-android/tree/master/database

的firebase示例代码

我是JSON的新手,我尝试了很多方法,并且无法找出返回由用户B主演的帖子(以便用户B可以看到他之前喜欢的帖子) 或者我是否需要构建另一棵树?

我试过查询:

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

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

}

我的JSON树:

{  
   "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"
      }
   }
}

2 个答案:

答案 0 :(得分:3)

首先,您缺少一个参考:

while

但是,直接进行搜索仍然非常困难甚至不可能,而无需筛选返回的数据。

我建议做的是创建一个// Old databaseReference.child("posts").child("stars").child(getUid()).equalTo(true); // New databaseReference.child("posts").child(some-post).child("stars").child(getUid()).equalTo(true); 树,并将已加星标的帖子写入特定用户而不是帖子的用户。 e.g:

users

(将新加星标的帖子推送到"users": { "userA": { "starred": { "-KMP3242nf23nfn23": { "author": "userB", "post": "-KMPiC6Okoe2dd35keT6" }, "-KMPiIHQXrZIfnv2uNV-": { "author": "userB", "post": "-KMPiC6Okoe2dd35keT6" }, ... } } }

然后你可以这样做一个查询:

starred

其他参考:Firebase - How do I write multiple orderByChild for extracting data?

评论任何问题。

答案 1 :(得分:1)

这是非常直接的深层查询

这是一个Swift解决方案(使用v2.x Firebase),因为未指定平台。

    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)")

            }
        }
    })

这将打印

key = -KMPiC6Okoe2dd35keT6  author = user A

如果还有其他节点也符合条件,它们也会打印出来。