简体   繁体   English

如何在不覆盖数据的情况下将文档存储在 Firestore 数据库中?

[英]How can I store documents in firestore database without overwritting the data?

I want to store "val credentials" inside "Credenciales" and "val generales" within "Generales" for each user in the firestore.我想为firestore中的每个用户将“val credentials”存储在“Credenciales”中,并将“val generales”存储在“Generales”中。 However, if I sign up as a new user, it overwrites the stored data of the old user.但是,如果我注册为新用户,它会覆盖旧用户的存储数据。 Please, let me know how I can store both variables without replacing them.请让我知道如何在不替换它们的情况下存储这两个变量。

Here's my snipped code:这是我截取的代码:

val generales = hashMapOf(
    "Nombre" to nameEditText.text.toString(),
    "Celular" to celularEditText.text.toString(),
    "Cedula" to cedulaEditText.text.toString(),
    "Oficio" to oficioEditText.text.toString(),
    "Estado civil" to auto_complete_txt.text.toString(),
    "Calle" to calleEditText.text.toString(),
    "Sector" to sectorEditText.text.toString(),
    "Edificio" to edificioEditText.text.toString(),
    "Apto" to numeroEditText.text.toString(),
    "Ciudad" to ciudadEditText.text.toString(),
    "Residencial" to resEditText.text.toString(),
    "Telefono" to telEditText.text.toString(),
    )

    db.collection("Usuarios").document("Generales")
        .set(datos)

val credentials = hashMapOf(
    "email" to emailEditText.text.toString(),
    "password" to passwordEditText.text.toString(),
        )

     db.collection("Usuarios").document("Credenciales")
       .set(credentials)

Update:更新:

val generales = hashMapOf(
        "Nombre" to nameEditText.text.toString(),
        "Celular" to celularEditText.text.toString(),
        "Cedula" to cedulaEditText.text.toString(),
        "Oficio" to oficioEditText.text.toString(),
        "Estado civil" to auto_complete_txt.text.toString(),
        "Calle" to calleEditText.text.toString(),
        "Sector" to sectorEditText.text.toString(),
        "Edificio" to edificioEditText.text.toString(),
        "Apto" to numeroEditText.text.toString(),
        "Ciudad" to ciudadEditText.text.toString(),
        "Residencial" to resEditText.text.toString(),
        "Telefono" to telEditText.text.toString(),
    )


    // Add a new document with a generated ID
    db.collection("Usuarios").document("Credenciales")
        **.add(generales)** // I get an unresolved reference error here.
        .addOnSuccessListener { documentReference ->
            Log.d(TAG, "DocumentSnapshot written with ID: Generales")
        }
        .addOnFailureListener { e ->
            Log.w(TAG, "Error adding document", e)
        }

You need to remove the .document("Credenciales") when adding and just have it as:您需要在添加时删除.document("Credenciales")并将其设置为:

db.collection("Usuarios").add(generales) //etc.

of if you want to add to a sub collection it will be something like:如果您想添加到子集合中,它将类似于:

db.collection("Usuarios").doc(docref).collection(subcollectionRef).add(generales)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将来自 firestore 数据库中特定集合的文档字段数据存储到 kotlin 中的字符串变量 - How can I store document field data from a particular collection in firestore database to a string variable in kotlin 如何将日历数据存储在数据库中 - How can I store a calendar data in database 插入行而不覆盖数据 - Insert row without overwritting data 我如何阅读 Firestore 中的所有文档,除了一个或多个? - How can I read all documents in Firestore except one or more? 如何将 firebase 数据库的 eventlistner 的数据快照存储在字符串中 - how can I store the data snapshot of a eventlistner of firebase database in a string 如何在 Firestore 中将文档分组? - How do I group documents together in Firestore? 如何查询具有如此多文档的firestore集合而不会出现flutter错误? - How do i query a collection of firestore that has so many documents without an error in flutter? 如何在Firestore中存储文档的用户状态? [等候接听] - How can I store User Presence for Document in Firestore? [on hold] 从 Firestore 数据库中的多个文档中检索数据 - Retrieve Data from multiple documents from Firestore Database 如何在不更新数据的情况下将数据存储到firebase数据库中? 并检索数据 - How do I store a data into the firebase database without updating it? and also retrieving the data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM