Home » Android » java – How to add values to Firebase Firestore without overwriting?

java – How to add values to Firebase Firestore without overwriting?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have two Activities, I am adding data to Firestore from these two activities individually. But, whenever I add second activity data to Firestore, it is overwriting the first activity data. I used below code in the two activities:

 firebaseFirestore.collection("Users").document(user_id).set(data)

How to stop overwriting? I want to save both Activities data in the same user_id.

How to&Answers:

I suggest you to add one more document or collection that it will be able to store more just one data values for single user.
You can create a document references for both activities:

firebaseFirestore.collection("Users").document(user_id+"/acitivity1").set(data);
//and  
firebaseFirestore.collection("Users").document(user_id+"/acitivity2").set(data);

Or you can create a sub-collection for it:

firebaseFirestore.collection("Users").document(user_id)
                  .collection("Activities").document("acitivity1").set(data);
//and
firebaseFirestore.collection("Users").document(user_id)
                  .collection("Activities").document("acitivity2").set(data);

More about hierarchical data there.

Answer:

There are two ways in which you can achieve this. First one would be to use a Map:

Map<String, Object> map = new HashMap<>();
map.put("yourProperty", "yourValue");
firebaseFirestore.collection("Users").document(user_id).update(map);

As you can see, I have used update() method instead of set() method.

The second approach would be to use an object of your model class like this:

YourModelClass yourModelClass = new YourModelClass();
yourModelClass.setProperty("yourValue");
firebaseFirestore.collection("Users").document(user_id)
    .set(yourModelClass, SetOptions.mergeFields("yourProperty"));

As you can see, I have used the set() method but I have passed as the second argument SetOptions.mergeFields("yourProperty"), which means that we do an update only on a specific field.

Answer:

If you know that the user document allready exists in firestore then you should use

firebaseFirestore.collection("Users").document(user_id).update(data)

If you don’t know if the document exists then you can use

firebaseFirestore.collection("Users").document(user_id).set(data, {merge:true})

This performs a deep merge of the data

Alternatively you can do it by using subcollections

Answer:

As per the documentation, you could use as a second parameter {merge:true}, in my experience the problem usually is in the fact that you are trying to store different data but with the same key.

Even using {merge: true} will always update the current key with the value you are passing in.

Merge:true Works only if the key does not exist already. I believe every key in a document must be unique.

To test it try to pass(keeping {merge: true} as the second parameter) data with a different key, it will merge to existing.

Answer:

Try this for a direct update

db.collection('cities').doc('restaurants').update({
rating:"3.2"
});

Only the field rating is going to change, the rest of the fields will remain the same.

Lets say you have 3 fields, and want to change only 2 of them, you can also do this in a different way

// You get the document 'restaurants' and then do the below
db.collection('cities').doc('restaurants').get().then((doc) => {

    // Get the rating field string, parse it to a float, remove 0.4 from it and
    // set it to a tmp variable
    var tmpRating = parseFloat(doc.data()['rating']) - 0.4;

    // Get the array of strings field 'menu' and set it to a tmp variable
    var tmpMenu = doc.data()['menu'];

    // Push 'spaghetti' string to the tmp array of strings( Can also put an
    // adapting string variable instead of a specific string like 'spaghetti'
    tmpMenu.push('spaghetti');

    // Update only the specific fields 'rating' and 'menu' and keep the rest unchanged.
    db.collection('cities').doc(doc.id).update({
        rating: toString(tmpRating), // Don't forget to separate them by commas
        menu: tmpMenu
    });
});