Ari
Ari

Reputation: 6159

Firestore delete key within nested dictionary (map)

I'm trying to delete keys in a nested dictionary that have values of 0:

{
"keyA": {
        "keyA1": 10,
        "keyA2": 0,
        "keyA3": 3,
    },
"keyB": {
        "keyB1": 2,
        "keyB2": 6,
        "keyB3": 0,
    },
}

I've tried every combination using .DELETE_FIELD

database.collection("cases").document("ari_test").update({
        f"someMap": {
            "keyA": {
                database.field_path("keyA2"): firestore.DELETE_FIELD
            }

        }
    })

I get the error:

ValueError: Cannot update with nest delete: FieldPath('someMap','keyA','`keyA2`')

Do i need to .get() the whole dict, update it in python then overwrite the whole map on firestore?

Upvotes: 0

Views: 442

Answers (1)

Juan Lara
Juan Lara

Reputation: 6854

Use a path with dot-notation to update nested fields, for example:

database.collection('cases').document('ari_test').update({
        'someMap.keyA.keyA2': firestore.DELETE_FIELD
        })

Upvotes: 4

Related Questions