【问题标题】:How to query to return post starred by specific user?如何查询返回由特定用户加星标的帖子?
【发布时间】:2016-07-11 16:24:47
【问题描述】:

我正在玩实时数据库的firebase示例代码https://github.com/firebase/quickstart-android/tree/master/database

我是 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"
      }
   }
}

【问题讨论】:

    标签: json firebase firebase-realtime-database


    【解决方案1】:

    首先,您缺少参考:

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

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

    我建议做的是创建一个users 树并将加星标的帖子写入特定用户而不是用户写入帖子。例如:

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

    (将新加星标的帖子推送到starred

    然后你可以这样查询:

    // 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);
    

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

    有任何问题发表评论。

    【讨论】:

    • 我明白了,但是 some-post 是添加新数据时随机生成的 post-id,我如何找到这个 post-id 变量?
    • 返回的帖子不是用户发的,不是用户加星的吗?
    【解决方案2】:

    这是非常直接的深度查询

    这是一个 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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 2016-02-15
      • 1970-01-01
      • 2020-12-27
      • 2021-06-10
      相关资源
      最近更新 更多