如何在不覆盖的情况下向 Firebase Firestore 添加值? [英] How to add values to Firebase Firestore without overwriting?

查看:30
本文介绍了如何在不覆盖的情况下向 Firebase Firestore 添加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动,我分别从这两个活动向 Firestore 添加数据.但是,每当我向 Firestore 添加第二个活动数据时,它都会覆盖第一个活动数据.我在两个活动中使用了以下代码:

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)

如何停止覆盖?我想将两个活动数据保存在同一个 user_id 中.

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

推荐答案

我建议您再添加一个文档或集合,它可以为单个用户存储更多的数据值.
您可以为这两个活动创建文档引用:

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);

此处详细了解分层数据.

这篇关于如何在不覆盖的情况下向 Firebase Firestore 添加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆