Firestore:set() 和 add() 之间的区别 [英] Firestore: difference between set() and add()

查看:31
本文介绍了Firestore:set() 和 add() 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firestore 中的 set()add() 有什么区别?

What is the difference between set() and add() in Firestore?

我使用 set() 将文档添加到我的集合中.但我无法使用 add() 或从文档中理解 add().

I use the set() to add documents to my collection. But I am unable to use add() or understand add() from docs.

推荐答案

既然你没有指定,我假设你的意思是 set() on DocumentReferenceadd() on CollectionReference.

Since you didn't specify, I'm going to assume you mean set() on DocumentReference, and add() on CollectionReference.

当您在 DocumentReference 上使用 set() 时,您将数据放入一个文档中,该文档已经被某些人识别唯一身份.(否则,您就不会拥有 DocumentReference 对象!)正如文档中所说,如果文档尚不存在,则会创建它."如果该文档已存在,您要么将新数据替换或合并到其中.

When you use set() on a DocumentReference, you're putting data into a document that you're already identified by some unique id. (Otherwise, you wouldn't already have a DocumentReference object!) As it says in the docs, "If the document does not yet exist, it will be created." If the document already exists, you're either replacing or merging new data into it.

当您在 CollectionReference 上使用 add() 时,您将无条件地在集合中创建一个新文档,并且该新文档将分配一个 唯一 id它.您传递的数据将成为新文档的内容.

When you use add() on a CollectionReference, you're unconditionally creating a new document in a collection, and that new document will have a unique id assigned to it. The data you pass will become the contents of the new document.

这篇关于Firestore:set() 和 add() 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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