How to add values to Firebase Firestore without ov

2019-02-22 06:06发布

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.

4条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-22 06:29

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.

查看更多
forever°为你锁心
3楼-- · 2019-02-22 06:30

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.

查看更多
▲ chillily
4楼-- · 2019-02-22 06:31

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.

查看更多
聊天终结者
5楼-- · 2019-02-22 06:36

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

查看更多
登录 后发表回答