如何有效地查找Firebase中一组节点中是否包含元素? [英] How do I efficiently find if one set of nodes has elements contained in another in Firebase?

查看:66
本文介绍了如何有效地查找Firebase中一组节点中是否包含元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个社交媒体数据库架构,其中包含用户,关注者,标签和帖子.为了符合Firebase模型,我按照Firebase文档中的建议对结构进行了展平,如下所示.我正在努力解决的问题是,当用户选择一个标签并从tagPosts表中看到一堆帖子时,这些帖子都与返回的标签相关,然后我想首先显示当前用户关注者创建的帖子.

I am building a social media database schema, in which I have users, followers, tags and posts. To conform to the firebase model I have flattened the structure as suggested in the firebase documentation as seen below. The issue that I am struggling with is when a user selects a tag and sees a bunch of posts from the tagPosts table all related by tag returned, I would then like to show the posts created by the current users followers first.

在SQL中,这将通过内联查询来完成,该内联查询根据特定标记返回的帖子检查用户关注者. 但是,在firebase中,我不确定如何执行以下操作,而不下载tagPosts中tagID节点下包含的所有帖子,并针对当前用户userID对照Followers节点检查每个帖子的创建者.对于100个用户中的100个帖子,此操作很容易失控.我已经尝试过根据以下答案进行建模:如何检查Firebase数据库值是否存在?,而本文来自从SQL到Firebase —如何为社交网络应用构建数据库.我是否在构造数据方面差强人意,如何解决这个问题呢?

In SQL this would be done with an inline query checking the users followers, against the posts returned by a specific tag. However in firebase I am not sure how do this without downloading all the posts contained under the tagID node in tagPosts and checking through each post's creator against the node of Followers for the current user userID. This operation could easily grow out of hand for 100s of posts amongst 100s of users. Ive tried modeling off of this answer, How do I check if a firebase database value exists? and this article From SQL to Firebase — How to structure the DB for a social network app. Am I poorly structuring the data how do I fix this thank you so much.

 `
Users-
     -userID1
         -misc. userData
     -userID2
          -misc. userData
Followers-
     -userID1
        -userIDOfFollower1   
        -userIDOfFollower2
Following-
     -userID1
        -userIDOfFollower1   
        -userIDOfFollower2

Posts-
    -postID1
       -userIDFromCreator
       -misc. PostData


Tags-
    -tagID1
       -misc. TagData
TagsUsers
    -tagID1
       -userID1
       -userID2
TagsPosts
     -tagID1
        -postID1
        -postID2

编辑-谢谢弗兰克

在我们的情节提要流中,我们计划让用户看到一堵标签墙,这些标签墙是根据标签的属性以及我们预测用户可能感兴趣的位置不断更新热门得分而确定的.然后,用户将选择一个标签并查看与该标签相关的信息,我希望这些信息显示给用户关注者的信息,而其他所有人的信息都属于指定标签的类别.

In our storyboard flow we plan to have a user see a wall of tags determined by constantly updating popular score based on properties of the tag and where we predict the user may have interest. The user will then select a tag and see posts related to that tag, from those posts I would like to show the posts from a users followers before those of everyone else who’s post falls in the category of a specified tag.

我考虑了两种可能性,或者我优化了读取效果,在这种情况下,我必须跟踪用户关注者每次发布到标签的时间,并在用户发布的每个关注者中将tagID连同postID一起记录在节点中在FollowersTags的特殊节点中,该节点的结构将为每个userID列出用户列表以及所发布用户的所有关注者,对于每个帖子,该列表将成为与好友所拥有的关注者数量成正比的每个帖子的100个写入次数.

I have considered two possibilities either I optimize on reads in which I would have to keep track of every time a users follower posts to a tag and record the tagID along with the postID in a node for every follower a user has who posted in a special node of FollowersTags which would have a structure of listing for each userID a list of users and the all the followers of a user posted to which would become 100s of writes for each post created directly proportional to the number of followers a friend has.

*creates a list of posts to a specific tag made by followers
FollowersTags
    -userID1_tagID1(composite key)
            -postID1
            -postID2
            -postID3
            -postID4
    -userID1_tagID2
            -postID1
            -postID2
            -postID3
            -postID4

或者我可以像上面尝试的那样优化写操作,这给我们带来了当前的困境,即必须执行100倍的查询时间,该查询次数与标签中的帖子数量成正比.

Or I could optimize on writes as tried above, which presents us with our current predicament of having to perform a query 100s of times directly proportional to the number of posts in a tag.

有没有办法解决这两个选项中哪个是更好的方法.

Is there any way around these two options which of the two is the better approach.

不幸的是,在他们选择标签之前,我无法预测显示给用户的帖子.

Unfortunately I would not be able to predict the posts displayed to the user before they select a tag.

推荐答案

在Firebase实时数据库中,我通常将数据库中的数据建模为屏幕上显示的数据.因此,如果每个用户都有最近相关帖子的墙",请考虑在数据库中进行精确建模:每个用户最近相关帖子的列表(或帖子ID).

In the Firebase Realtime Database, I typically model the data in the database to what I show on the screen. So if you have a "wall" of recent, relevant posts for each user, consider modeling precisely that in your database: a list of recent, relevant posts (or post IDs) for each user.

UserWalls
  userID1
    "timestamp_or_push_id": "postId1"
    "timestamp_or_push_id": "postId2"
  userID2
    "timestamp_or_push_id": "postId1"
    "timestamp_or_push_id": "postId3"

虽然确定要显示的内容的问题仍然相同,但对于此数据库模型,现在是写时问题,而不是读时问题.

While the problem of determining what to show remains the same, with this database model it's now a write-time problem, instead of a read-time problem.

这篇关于如何有效地查找Firebase中一组节点中是否包含元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆