Codist
Codist

Reputation: 769

How to skip a field in Firestore when updating a document?

I have @ServerTimestamp var product_added_date: Date? = null, in my model data class in Kotlin. When I update the product, later on, the value of product_added_date gets updated. I want to skip this field whenever I update the document. How can I do that? I googled for this but I couldn't find it.

Edit:

Following is the code I use to update the details.

    fun updateProduct(activity: ProductActivity, productDetails: Product, productId: String) {
    mFireStore.collection(Constants.PRODUCTS)
        .document(productId)
        .set(productDetails, SetOptions.merge())
        .addOnSuccessListener {
            activity.updatingProductSuccess()
        }
        .addOnFailureListener { e ->
            activity.hideProgressDialog()
        }
}

Upvotes: 0

Views: 541

Answers (2)

Anas mugally
Anas mugally

Reputation: 19

You can update specific field via use mapOf in kotlin as this

FirebaseFirestore.getInstance().document(productId).set(mapOf(
    "key1" to "string value",
    "key2" to "int Value",
    "name" to productDetails.name,
    "price" to productDetails.price,
    "key5" to "Any Value"
),SetOptions.merge())

Change all key string to your key for product object in firestore and set any value.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599591

There is no way to perform a partial update when you pass a Java class to the set() method - as Firestore has no way of knowing which fields you want to update and which ones you don't.

When you want to perform a partial update, put the fields you want to update in a Map<String,Object> and pass that to .set(map, SetOptions.merge()).

Upvotes: 1

Related Questions