【问题标题】:Structure Database Firestore Posts结构数据库 Firestore 帖子
【发布时间】:2017-10-17 17:23:00
【问题描述】:

实时 x Firestore 数据库结构

实时系统结构帖:

Posts
--ID_USER_1
----IdPost1
------Date
------Image
---IdPost2
------Date
------Image
---IdPost3
------Date
------Image
--ID_USER_2
----IdPost1
------Date
------Image
---IdPost2
------Date
------Image
---IdPost3
------Date
------Image

实时发布系统模型。要在 Firestore 中构建,它可能看起来像这样:

Post
--ID_POST1
----Id_user1
----Image
--ID_POST2
----Id_user2
----Image

据我了解,Firestore 具有集合的约束,因为当转换为相同的 Real Time 结构时,它不起作用,因为我们不能有两个集合,例如:mDB.collection ("Posts") .collection().

在用户之间构建发布系统的最佳方式是什么?

谢谢!!

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    您是正确的,在 Cloud Firestore 中您必须在文档和集合之间交替,但我认为这不会对您的数据结构造成问题。

    例如,您可以:

      - `posts` [collection]
        - `ID_USER_1` [document]
          - `posts` [subcollection]
             - `ID_POST_1` [document]
               - `Date` [field]
               - `Image` [field]
             - `ID_POST_2` [document]
               - `Date` [field]
               - `Image` [field]
    

    因此,要获取用户的所有帖子,您可以这样做:

    db.collection('posts').doc('user1234').collection('posts').get()
    
    // OR
    
    db.collection('posts/user1234/posts').get()
    

    由于 Cloud Firestore 还具有快速而强大的查询功能,您还可以将所有帖子存储在一个顶级 posts 集合中,并将 UserId 存储为帖子的属性:

      - `posts` [collection]
         - `ID_POST_1` [document]
           - `UserId` [field]
           - `Date` [field]
           - `Image` [field]
         - `ID_POST_2` [document]
           - `UserId` [field]
           - `Date` [field]
           - `Image` [field]
    

    然后要获取用户的所有帖子,您可以这样做:

    db.collection('posts').where('UserId', '==', 'user1234')
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2019-09-18
    • 2020-12-05
    • 2023-03-26
    相关资源
    最近更新 更多