Cloud Firestore合并true不工作在nodejs中

发布于 2025-02-11 03:54:18 字数 280 浏览 0 评论 0 原文

在firestore文档中,它说合并:true 如果要合并数据,它在这里不起作用

是我的代码,它与示例非常相似,它总是替换旧存储的值使用新的,

async function updateTotal() {
  const update = await db.collection("resources").doc(id).update({ "total": "500"}, { merge: true })
}

如果您有解决方案,我真的需要它

In firestore documentation it says that merge: true is needed if we want to merge data, it doesn't work

Here is my code, which is very similar to the example, it always replaces the old stored value ​​with the new one

async function updateTotal() {
  const update = await db.collection("resources").doc(id).update({ "total": "500"}, { merge: true })
}

I really need it if you have a solution please

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亽野灬性zι浪 2025-02-18 03:54:18

如此其他答案

  • set merge 将始终用您通过的数据覆盖数据,而
  • Update 是专门设计的,可以使您有可能执行文档的部分更新,而无需创建您的代码不准备处理的不完整文档。请检查 this 答案,以及 this scenario。

这就是 nick-wnick 在他的评论

这是另一个答案更好地解释了每个选项的用例:

  • set 没有 MERGE 将覆盖文档或创建它,如果它不存在
  • set 带有 MERGE 将在文档中更新字段,或者如果不存在,则创建它
  • 更新将更新字段,但是如果文档不存在,将失败
  • 创建将创建文档,但是如果文档已经存在
  • ,则会失败


,您应该使用 ,如下:

您可以按照以下示例显示的数字字段值递增或降低数字字段值。增量操作增加或降低了字段的当前值。

  //原子上将城市人口增加50。  
washingtonref.update({ 
   人口:firebase.firestore.fieldvalue.Increment(50)  
});
 

另请参见:

As shown on this other answer:

  • set merge will always override the data with the data you pass, while
  • update is specifically designed to give you the possibility to perform a partial update of a document without the possibility of creating incomplete documents that your code isn't otherwise prepared to handle. Please check this answer, as well as this scenario.

This is what nick-w-nick explains in his comment.

This another answer explains better the use case for each option:

  • set without merge will overwrite a document or create it if it doesn't exist yet
  • set with merge will update fields in the document or create it if it doesn't exists
  • update will update fields but will fail if the document doesn't exist
  • create will create the document but fail if the document already exists

For the expected result you explained in your comment, you should be using an increment operation, as follows:

You can increment or decrement a numeric field value as shown in the following example. An increment operation increases or decreases the current value of a field by the given amount.

// Atomically increment the population of the city by 50.  
washingtonRef.update({ 
   population: firebase.firestore.FieldValue.increment(50)  
});

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文