Firestore:set()和add()之间的区别 [英] Firestore: difference between set() and 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.
推荐答案
由于您未指定,因此我假设您的意思是添加到CollectionReference上的.
Since you didn't specify, I'm going to assume you mean set() on DocumentReference, and add() on CollectionReference.
在DocumentReference
上使用set()
时,您会将数据放入已通过某些唯一ID 已标识的文档中. (否则,您将没有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屋!