gpt4 book ai didi

java - 云Firestore : Add Field Value To Document

转载 作者:行者123 更新时间:2023-12-01 19:01:08 25 4
gpt4 key购买 nike

我想要创建一个名为 question_count 的字段值,并且想要增加该值并将其添加到我的云 Firestore 文档中。我已经尝试过了:

document_reference.update("question_count", FieldValue.increment(1))

问题:我必须手动将 question_count 字段添加到我的云 Firestore 文档中,然后上述代码将增加该值。 Cloud Firestore Dashboard我想要的:有人告诉我,如果尚未在云 Firestore 中创建字段值,则使用 DocumentReference update(...) 以及指定的字段值将创建该字段值。这是一个错误的假设吗?

最佳答案

update该方法仅在文档已经存在时才有效。如果您想要执行写入操作, 创建文档 在文档已存在时对其进行更新,请使用 docRef.set(..., SetOptions.merge()) .

所以在你的情况下,类似:

// Update one field, creating the document if it does not already exist.
Map<String, Object> data = new HashMap<>();
data.put("question_count", FieldValue.increment(1));

document_reference.set(data, SetOptions.merge());

另请参阅 writing data 上的文档.

关于java - 云Firestore : Add Field Value To Document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59637252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com