How to update a dynamic object property in Firestore

If you want to update a dynamic property of an object using Firestore without overlapping the current data, set {merge: true}:

javascript
const today = '20210323';

const visitorId = ~~(1000 * Math.random());

const db = firebase.firestore();

db.collection('analytics').doc(today).set({
    
    [visitorId]: {
        visited: true
    }
    
}, {merge: true}); 

If you don't include {merge: true}, you'll overlap the current data.

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.