Home > other >  Adding to Firestore collection
Adding to Firestore collection

Time:10-16

I want to add to the "receipts" collection a document with a unique "id: and for this document the data:" id "," from "," value "and" image ". Firebase !!important !!code in Fragment

CodePudding user response:

I know there is no such command in Firebase. Therefore, you should take the data you entered and search it in Firestore. You can perform the operation according to the condition.

CodePudding user response:

If you need to add a document to a specific collection with a unique ID, then you should use the DocumentReference#set() method which:

Writes to the document referred to by this DocumentReference.

In code, it should look like this:

val db = FirebaseFirestore.getInstance()
val receiptsRef = db.collection("receipts")
val docId = receiptsRef.document().id
val receipt = mapOf(
    "id" to docId,
    "from" to "Name",
    "value" to "The value of the receipt",
    "image" to "URL of the image"
)
receiptsRef.document(docId).set(receipt)

You can also use addOnCompleteListener(OnCompleteListener listener), to see if something goes wrong.

  • Related