database set with {merge: true} 和 update 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46597327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between set with {merge: true} and update
提问by ZuzEL
In Cloud Firestorethere are three write operations:
在Cloud Firestore 中,有三种写入操作:
1) add
1) 添加
2) set
2) 设置
3) update
3) 更新
In the docs it says that using set(object, {merge: true})will merge object with existing one.
在文档中,它说 usingset(object, {merge: true})会将对象与现有对象合并。
The same happens when you use update(object)So what is the difference if any? It seems strange that google will duplicate logic.
使用时update(object)也会发生同样的情况那么有什么区别呢?谷歌会复制逻辑似乎很奇怪。
回答by Scarygami
The way I understood the difference:
我理解差异的方式:
setwithoutmergewill overwrite a document or create it if it doesn't exist yetsetwithmergewill update fields in the document or create it if it doesn't existsupdatewill update fields but will fail if the document doesn't existcreatewill create the document but fail if the document already exists
setwithoutmerge将覆盖文档或创建它(如果它尚不存在)setwithmerge将更新文档中的字段,如果不存在则创建它update将更新字段但如果文档不存在则会失败create将创建文档但如果文档已经存在则失败
There's also a difference in the kind of data you provide to setand update.
您提供给set和的数据类型也有所不同update。
For setyou always have to provide document-shaped data:
因为set您总是必须提供文档形状的数据:
set(
{a: {b: {c: true}}},
{merge: true}
)
With updateyou can also use field paths for updating nested values:
随着update你也可以使用现场路径用于更新嵌套值:
update({
'a.b.c': true
})
回答by Finlay Percy
Another difference (extending Scarygami's answer) between "set with merge" and "update", is when working with a nested values.
“设置合并”和“更新”之间的另一个区别(扩展 Scarygami 的答案)是在使用嵌套值时。
if you have a document structured like this:
如果您的文档结构如下:
{
"friends": {
"friend-uid-1": true,
"friend-uid-2": true,
}
}
and want to add {"friend-uid-3" : true}
并想添加 {"friend-uid-3" : true}
using this:
使用这个:
db.collection('users').doc('random-id').set({
"friends": {
"friend-uid-3": true
}
},{merge:true})
db.collection('users').doc('random-id').set({
"friends": {
"friend-uid-3": true
}
},{merge:true})
will result in this data:
将导致此数据:
{
"friends": {
"friend-uid-1": true,
"friend-uid-2": true,
"friend-uid-3": true
}
}
however updateusing this:
但是update使用这个:
db.collection('users').doc('random-id').update({
"friends": {
"friend-uid-3": true
}
})
db.collection('users').doc('random-id').update({
"friends": {
"friend-uid-3": true
}
})
will result in this data:
将导致此数据:
`{
"friends": {
"friend-uid-3": true
}
}`
回答by CodeManDan
Per docs: https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects
每个文档:https: //firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects
Dot notation allows you to update a single nested field without overwriting other nested field. If you update a nested field without dot notation, you will overwrite the entire map field.
点表示法允许您在不覆盖其他嵌套字段的情况下更新单个嵌套字段。如果您更新没有点符号的嵌套字段,您将覆盖整个地图字段。
As stated above, this replaces entire friends structure.
如上所述,这取代了整个朋友结构。
db.collection('users').doc('random-id').update({
"friends": {
"friend-uid-3": true
}
})
This does not.
这不。
db.collection('users').doc('random-id').update({
"friends.friend-uid-3": true
})

