且构网

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

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

更新时间:2023-01-29 13:37:14

由于您未指定,因此我假设您的意思是添加到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.