Firestore – 使用 update 方法更新带有对象的文档(Java)

huangapple go评论77阅读模式
英文:

Firestore - Use update method for document but with Object (Java)

问题

我想使用我拥有的 User 对象更新文档,但如果文档不存在,我不希望创建文档,因此我不能使用 "DocumentReference.set" 并附带 "SetOptions.Merge()"(据我理解)。

然而,根据这个帖子(链接:https://stackoverflow.com/questions/46597327/difference-between-set-with-merge-true-and-update),实际上我需要使用 "update" 命令。我的问题是,似乎 "update" 不接受 Java 对象。

我不想自己检查文档是否存在,因为这会导致不必要的读取操作。

是否有任何解决方法?

以下是我的代码(出于简化的目的,我已删除了成功和失败的监听器):

public void saveUser(User user)
{
    CollectionReference collection = db.collection("users");

    String id = user.getId();
    if (id.equals(""))
    {
        collection.add(user);
    }
    else
    {
        // 我需要确保我的用户的 ID 变量与现有的 ID 对应,
        // 因为我不希望由我的 Java 代码生成新的 ID
        // (所有的 ID 应该由 Firestore 自动生成)
        collection.document(ID).set(user);
    }
}
英文:

I want to update a document with a User object that I have, but I do not want the document to be created if it does not exist, and therefore I cannot use "DocumentReference.set" with "SetOptions.Merge()" (to my understanding).

However, according to this post (https://stackoverflow.com/questions/46597327/difference-between-set-with-merge-true-and-update), "update" is actually the command I need. My problem is, it doesn't seem like update accepts a Java object.

I do not want to check whether or not the document exists myself, as this will result in an unnecessary read.

Is there any way around this?

Here is my code (I have removed success and failure listeners for simplicity):

    public void saveUser(User user)
	{
		CollectionReference collection = db.collection("users");

		String id = user.getId();
		if (id.equals(""))
		{
			collection.add(user);
		}
		else
		{
			// I need to ensure that the ID variable for my user corresponds
            // with an existing ID, as I do not want a new ID to be generated by 
            // my Java code (all IDs should be generated by Firestore auto-ID)
            collection.document(ID).set(user);
		}
	}

答案1

得分: 1

听起来你:

  1. 想要更新现有文档
  2. 不确定它是否已经存在
  3. 不愿意阅读文档以查看是否存在

如果是这种情况,只需调用update(),如果文档不存在,让它失败。这不会导致你的应用崩溃。只需将错误监听器附加到返回的任务,并在其失败时决定你想要执行的操作。

然而,你需要使用你拥有的源对象构建一个字段和值的映射,以便进行更新。对此没有变通方法。

英文:

It sounds like you:

  1. Want to update an existing document
  2. Are unsure if it already exists
  3. Are unwilling to read the document to see if it exists

If this is the case, simply call update() and let it fail if the document doesn't exist. It won't crash your app. Simply attach an error listener to the task it returns, and decide what you want to do if it fails.

However you will need to construct a Map of fields and values to update using the source object you have. There are no workarounds for that.

huangapple
  • 本文由 发表于 2020年7月25日 00:00:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63077236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定