Firestore NoSql关注和关注者系统

问题描述

假设一个用户拥有追随者并且也关注其他用户的常规网络。我正在尝试为此建立良好的数据库结构。到目前为止,我一直在这样做:

/
---users (collection)
|---------- userId (document)
|          |-------------- document data
|---------- user1Id (document)
           |-------------- document data
            ...

---following
|---------- userId (document)
|          |---------------- users (collection) <--------- The users that userId follows
|                            | ----------- user1Id (document)
|                            | ----------- user2Id (document)
|---------- user1Id (document)
           |--------------- users (collection) 
                             | ----------- user2Id (document)
                             | ----------- user3Id (document)


     

使用这种数据库结构,我可以在以下集合中存储无限数量用户,并成功查看该用户当前正在关注的每个用户。问题出在我想查看用户的关注者时。我怀疑是否会在我的“跟随”集合中的所有“ userId”文档中创建另一个集合“ userFollowers”,该集合表示特定用户的关注者,我的意思是,与“ “关注”,或者最好不要在与“关注”相同的级别上创建它。

我不知道,我是Firestore和Nosql数据库的新手,这是解决此问题的常用方法还是有更好的选择?

解决方法

只需回答这个问题...

如上所述,@ conchi-bermejo可以使用db.collection("following").document(userId).collection().get()

正如docs所提到的,这是获取这些文档的另一种方式:

collections = db.collection("following").document(userId).collections()
for collection in collections:
    for doc in collection.stream():
        print(f'{doc.id} => {doc.to_dict()}')

提示,文档是您入门时最好的朋友c:

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...