使用{merge:true}设置和更新之间的区别 [英] Difference between set with {merge: true} and update

查看:97
本文介绍了使用{merge:true}设置和更新之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

使用<$ c $时也会发生同样的情况。 c> 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 without merge will overwrite a document or create it if it doesn't exist yet

set 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

您提供给<$的数据类型也有所不同c $ c> set 和 update

对于设置,您总是必须提供文档形数据:

For set you always have to provide document-shaped data:

set(
  {a: {b: {c: true}}},
  {merge: true}
)

通过更新您还可以使用用于更新嵌套值的字段路径:

With update you can also use field paths for updating nested values:

update({
  'a.b.c': true
})

这篇关于使用{merge:true}设置和更新之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆