set with {merge: true} 和 update 的区别 [英] Difference between set with {merge: true} and update
问题描述
在 Cloud Firestore 中有三个写入操作:
In Cloud Firestore there are three write operations:
1) 添加
2) 设置
3) 更新
在文档中它说使用 set(object, {merge: true})
会将对象与现有对象合并.
In the docs it says that using set(object, {merge: true})
will merge object with existing one.
使用 update(object)
时也会发生同样的情况那么有什么区别呢?谷歌会重复逻辑似乎很奇怪.
The same happens when you use update(object)
So what is the difference if any? It seems strange that google will duplicate logic.
推荐答案
我理解差异的方式:
set
没有merge
将覆盖一个文档或创建它,如果它还不存在
set
withoutmerge
will overwrite a document or create it if it doesn't exist yet
set
with merge
将更新文档中的字段,如果不存在则创建它
set
with merge
will update fields in the document or create it if it doesn't exists
update
将更新字段,但如果文档不存在则会失败
update
will update fields but will fail if the document doesn't exist
create
将创建文档但如果文档已经存在则失败
create
will create the document but fail if the document already exists
您提供给 set
和 update
的数据类型也有所不同.
There's also a difference in the kind of data you provide to set
and update
.
对于set
,你总是需要提供文档形状的数据:
For set
you always have to provide document-shaped data:
set(
{a: {b: {c: true}}},
{merge: true}
)
通过 update
,您还可以使用字段路径来更新嵌套值:
With update
you can also use field paths for updating nested values:
update({
'a.b.c': true
})
这篇关于set with {merge: true} 和 update 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!