Android 结构数据库Firestore帖子
实时x Firestore数据库结构 系统结构实时发布:Android 结构数据库Firestore帖子,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,实时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 ------Da
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中存在集合的限制,因为当转换为相同的实时结构时,它将不起作用,因为我们不能有两个集合,如: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]
因此,要获得用户的所有帖子,您可以执行以下操作:
db.collection('posts').doc('user1234').collection('posts').get()
// OR
db.collection('posts/user1234/posts').get()
db.collection('posts').where('UserId', '==', 'user1234')
由于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').doc('user1234').collection('posts').get()
// OR
db.collection('posts/user1234/posts').get()
db.collection('posts').where('UserId', '==', 'user1234')
我很好奇我会如何处理这个案子: