java - 如何构建博客 API 多对多关系的 Firestore JSON
问题描述
Firebase/FireStore 新手,没有NoSQL
.
通过为自己构建应用程序来学习 Android 和空间时,我意识到我需要一个实时数据库解决方案。但我坚持构建模型结构。
这是 JSON 格式:
{
"published": "date",
"title": "This is the title",
"content": "This is the body",
"tags": [
"tag1", "tag2"
]
}
问题在于tags
多对多关系。意识到 FireStore 可以通过索引解决这个问题,但是我如何使用集合和文档来构造数据呢?谁能详细解释一下?
解决方案
假设您问题中的片段是一个post
对象,我建议您使用如下所示的数据库模式:
Firestore-root
|
--- posts (collection)
|
--- postId (document)
|
--- published: "date"
|
--- title: "This is the title"
|
--- content: "This is the body"
|
--- tags
|
--- tag1: true
|
--- tag2: true
根据有关Cloud Firestore 数据库中建模数据的官方文档:
每个文档都包含一组键值对。Cloud Firestore 针对存储大量小文档进行了优化。
所以这是我能为您的应用程序想到的最佳数据库结构,正如您所看到的,该tags
属性不是 an array
is a map
。这是此类对象的最佳实践。另请参阅有关使用数组、列表和集合的官方文档。
2018 年 8 月 13 日编辑:
根据有关数组成员资格whereArrayContains()
的更新文档,现在可以使用方法根据数组值过滤数据。一个简单的例子是:
CollectionReference citiesRef = db.collection("cities");
citiesRef.whereArrayContains("regions", "west_coast");
此查询返回区域字段是包含 west_coast 的数组的每个城市文档。如果数组具有您查询的值的多个实例,则该文档仅包含在结果中一次。
推荐阅读
- python - Telnet 错误地发送了 unicode 字符
- php - Kubernetes 内的 Nginx fpm Laravel 容器未获取环境变量
- batch-file - cURL 请求挂起
- python-3.x - 交换列值然后将它们附加到熊猫数据框中的更好方法?
- azure - 在没有网站的情况下,在 IIS 中将一个域安全地重定向到另一个域的最佳方法
- loops - 复合字面量
- reactjs - 无法访问环境变量 React
- docker - docker 桌面上的设备上没有剩余空间用于 macos 和 skaffold
- sql - 显示国家名称中至少包含 2 个单词的所有国家
- html - 如何将不同大小的图像放入相同大小的 CSS 网格中,同时保持图像的纵横比?