Firebase Firestore:是否可以对集合中的所有文档强制执行必填字段? [英] Firebase Firestore: Is there a way to enforce required fields for all documents in a collection?

查看:62
本文介绍了Firebase Firestore:是否可以对集合中的所有文档强制执行必填字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为正在创建的应用程序手动添加数据.我必须将许多文档添加到集合中,所有文档都必须包含相同的5个字段.在Firebase控制台中创建新文档时,是否可以自动创建这些字段?还要强制执行所有具有这5个字段的文档(这样我就不会犯错误)?

I am manually adding data for an app I am creating. I have to add many documents to a collection, all of which have to contain the same 5 fields. Is there a way to automatically create these fields when I create a new document in the Firebase console? And to also enforce all documents have those 5 fields (so that I don't make mistakes) ?

此外,是否有比从Firebase控制台手动添加此数据更好的方法?例如通过JSON?

Additionally, is there a better way to manually adding this data than from the Firebase console? Through JSON for example?

推荐答案

此处有两个独立的问题.两者都已经被回答过,但是从来没有一个问题,所以我在这里做.

There are two separate questions here. Both have been answered before, but never in a single question, so I'll do that here.

这是不可能的.您已经将这些值明确地写入了所创建的每个新文档中.

This is not possible. You will have explicitly write those values into each new document you create.

这可以通过使用Firebase的服务器端安全规则来实现.

This is possible through the use of Firebase's server-side security rules.

例如,要确保文档具有field1field2field3的值,可以使用

For example, to ensure a document has values for field1, field2 and field3, you could use hasAll:

allow write: if resource.data.keys().hasAll(['editor', 'admin']);

这篇关于Firebase Firestore:是否可以对集合中的所有文档强制执行必填字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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