问题描述
假设一个用户拥有追随者并且也关注其他用户的常规网络。我正在尝试为此建立良好的数据库结构。到目前为止,我一直在这样做:
/
---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: