如何构建博客API多对多关系的Firestore JSON [英] How do I structure the Firestore JSON of a blog API many-to-many relation

查看:50
本文介绍了如何构建博客API多对多关系的Firestore JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase/FireStore的新手,并且没有使用NoSQL的经验.

New to Firebase/FireStore and have no experience in NoSQL.

在学习Android以及为自己构建应用程序的空间时,我意识到我需要一个实时数据库解决方案.但是我坚持建立模型结构.

While learning Android and as well as room by building an app for myself I realized that I need a real-time database solution. But I'm stuck at building the model structure.

这是JSON格式:

{
 "published": "date",
 "title": "This is the title",
 "content": "This is the body",
 "tags": [
  "tag1", "tag2"
 ]
}

问题在于tags多对多关系.意识到FireStore可以通过索引解决此问题,但是如何使用收藏夹"和文档"构造数据?谁能详细解释?

The problem is with the tags many-to-many relation. Realized that FireStore can solve this with indexation but how do I structure the data with Collections and Documents? Can anyone explain in details?

推荐答案

假设问题中的代码段是post对象,我建议您使用如下所示的数据库架构:

Assuming that the snippet in your question is a post object, I recommend you a database schema that looks like this:

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数据库中有关建模数据的官方文档进行操作:

Accordind to the official documentation regarding modelling data in a Cloud Firestore database:

每个文档包含一组键值对. Cloud Firestore经过优化,可存储大量小文件.

Each document contains a set of key-value pairs. Cloud Firestore is optimized for storing large collections of small documents.

所以这是我能想到的最适合您的应用程序的数据库结构,并且您可以看到,tags属性不是array还是map.这是涉及此类对象的最佳实践.请另请参阅有关使用数组,列表和集合的官方文档.

So this is the best database structure I can think of for your app and as you can see, the tags property is not an array is a map. This the best practice when it comes to this kind of objects. Please aslo see the offical documentation regarding working with arrays, lists, and sets.

编辑2018年8月13日:

根据有关数组成员身份的更新文档,现在可以使用whereArrayContains()方法基于数​​组值过滤数据.一个简单的例子是:

According to the updated documentation regarding array membership, now it is possible to filter data based on array values using whereArrayContains() method. A simple example would be:

CollectionReference citiesRef = db.collection("cities");
citiesRef.whereArrayContains("regions", "west_coast");

此查询返回每个城市文档,其中region字段是包含west_coast的数组.如果该数组具有要查询的值的多个实例,则该文档仅包含在结果中一次.

This query returns every city document where the regions field is an array that contains west_coast. If the array has multiple instances of the value you query on, the document is included in the results only once.

这篇关于如何构建博客API多对多关系的Firestore JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆