且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firestore:set() 和 add() 之间的区别

更新时间:2023-01-29 13:18:30

既然你没有指定,我假设你的意思是 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.