如何将子集合添加到 Firebase Cloud Firestore 中的文档 [英] How to add subcollection to a document in Firebase Cloud Firestore

查看:23
本文介绍了如何将子集合添加到 Firebase Cloud Firestore 中的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档没有任何关于如何将子集合添加到文档的示例.我知道如何将文档添加到集合以及如何向文档添加数据,但是如何将集合(子集合)添加到文档中?

不应该有这样的方法:

dbRef.document("example").addCollection("subCollection")

解决方案

Edit 13 Jan 2021:

根据关于

要在文档中编写subCollection,请使用以下代码:

DocumentReference messageRef = db.collection("rooms").document("roomA").collection("messages").document("message1");

如果您想创建 messages 集合并调用 addDocument() 1000 次肯定会很昂贵,但这就是 Firestore 的工作方式.如果需要,您可以切换到 Firebase 实时数据库,其中写入次数无关紧要.但是关于 Firestore 中支持的数据类型,其实你可以使用数组,因为支持.在 Firebase 实时数据库 中,您还可以使用 array,但这是一种反模式.Firebase 建议不要使用数组的众多原因之一是它使安全规则无法编写.

Cloud Firestore 可以存储数组,它不支持查询数组成员或更新单个数组元素.但是,您仍然可以利用 Cloud Firestore 的其他功能对此类数据进行建模.这里是文档,解释得很好.

您也不能创建一个包含 1000 条消息的子集合并将它们全部添加到数据库中,同时考虑单个记录.对于每条消息,它将被视为一次写入操作.总共 1000 次操作.上图没有显示如何检索数据,它显示了一个数据库结构,其中包含如下内容:

集合 ->文件 ->子集合 ->文档

The documentation does not have any examples on how to add a subcollection to a document. I know how to add document to a collection and how to add data to a document, but how do I add a collection (subcollection) to a document?

Shouldn't there be some method like this:

dbRef.document("example").addCollection("subCollection")

解决方案

Edit 13 Jan 2021:

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");

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.


Assuming we have a chat application that has a database structure that looks similar to this:

To write a subCollection in a document, please use the following code:

DocumentReference messageRef = db
    .collection("rooms").document("roomA")
    .collection("messages").document("message1");

If you want to create messages collection and call addDocument() 1000 times will be expensive for sure, but this is how Firestore works. You can switch to Firebase Realtime Database if you want, where the number of writes doesn't matter. But regarding Supported Data Types in Firestore, in fact, you can use an array because is supported. In Firebase Realtime database you could also use an array, but this is which is an anti-pattern. One of the many reasons Firebase recommends against using arrays is that it makes the security rules impossible to write.

Cloud Firestore can store arrays, it does not support querying array members or updating single array elements. However, you can still model this kind of data by leveraging the other capabilities of the Cloud Firestore. Here is the documentation where is very well explained.

You also cannot create a subcollection with 1000 messages and add all of them to the database and at the same time to consider a single record. It will be considered one write operation for every message. In total 1000 operations. The picture above does not show how to retrieve data, it shows a database structure in which you have something like this:

collection -> document -> subCollection -> document

这篇关于如何将子集合添加到 Firebase Cloud Firestore 中的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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