Kostya Lotysh
Kostya Lotysh

Reputation: 39

how to add id of document in filds firestore

i have firestore database where is a field which call idDoc after create a document i want to add a document id in idDoc i do this function

    fun updateNotesDocId(userId: String){
        notesChannnelsCollectionRef.document(userId)
            .collection("notes")
            .document(notesChannnelsCollectionRef.document().id)
            .update(mapOf(notesChannnelsCollectionRef.document().id to "idDoc"))

and this code doesnt work. I swapped everything.

whats i want in photo enter image description here

Upvotes: 0

Views: 47

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317487

Each call to document() with no parameters will give you a DocumentReference with a different random ID. You will want to just call it once, remember the result, and reuse it.

        val ref = notesChannnelsCollectionRef.document()
        notesChannnelsCollectionRef.document(userId)
            .collection("notes")
            .document(ref.id)
            .set(mapOf("idDoc" to ref.id))

Upvotes: 1

Related Questions