结构数据库 Firestore 帖子

Structure Database Firestore Posts

实时 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

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

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

据我所知,Firestore 有集合的约束,因为当转换为相同的实时结构时,它不会工作,因为我们不能有两个集合,如:mDB.collection ("Posts").collection().

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

谢谢!!

您说得对,在 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]

因此,要获取某个用户的所有 post,您可以这样做:

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

// OR

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

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

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

然后要获取某个用户的所有 post,您可以执行以下操作:

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