如何在 kotlin 的 firestore 中向文档添加额外数据?

How do you add extra data to a document in firestore in kotlin?

这就是我向 Firestore 添加数据的方式,希望它能让您了解我的数据库的外观:

val postMap = hashMapOf(
    "date" to currentDate,
    "publisher" to userID,
    "postImage" to myUrl,
    "caption" to binding.etCaption.text.toString().lowercase(),
    "rent" to binding.etHousePrice.text.toString().lowercase(),
    "location" to binding.etHouseLocation.text.toString().lowercase(),
    "rooms" to binding.etNumberOfRooms.text.toString().lowercase(),
    "description" to binding.etPostDescription.text.toString().lowercase()
)

我想再添加一条数据如下但是稍后

val data = hashMapOf("post_id" to id)

如何在不覆盖现有数据的情况下实现这一点?

稍后要将以下数据添加到现有文档中,请使用以下代码行:

val data = hashMapOf("post_id" to id)
val db = Firebase.firestore
val postsRef = db.collection("posts")
val postIdRef = postsRef.document(postId)
postIdRef.update(data)

更多详情如下:

https://firebase.google.com/docs/firestore/manage-data/add-data#update-data

如文档所述,您还可以附加一个故障列表器,以始终查看是否出现问题。

private var userMap: HashMap<String, Any> = HashMap() 
userMap["post_id"] = id
val collection = Firebase.firestore.collection("posts")
        collection.document(postId)
            .update(userMap)
            .addOnSuccessListener {
                //succes update message
            }
            .addOnFailureListener {
                //failure update message
            }