java - 如何将多个数据添加到 Android Firestore 中的一个文档?

标签 java android firebase google-cloud-firestore

数据仅存储在list/0中。 我希望将多个数据存储在用户的电子邮件中。

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseFirestore db = FirebaseFirestore.getInstance(); 

final ArrayList<AddDayInfo> list = new ArrayList<>();
final Map<String,Object> docData = new HashMap<>();
AddDayInfo memberInfo = new AddDayInfo(Type,Day,Name,Time1,Time2,Time3);
list.add(memberInfo);

for(int i=0;i<list.size();i++) {
    if (user != null) {
        docData.put("Day_M", list);
        db.collection("day").document(user.getEmail()).set(docData, SetOptions.merge())
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        startToast("Okay. Add Data..");
                        myStartActivity(Add_Main_Activity.class);
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        startToast("Failed...");
                    }
                });
    }
}

我想添加如下图所示的数据:

enter image description here

最佳答案

要将项目添加到 Firestore 中的数组中,您有两种选择:

  • 使用 FieldValue.arrayUnion 确保数组中只有唯一的元素。
  • 读取整个数组,在应用程序代码中更改它,然后写回。

这两者之间的选择取决于您的数据的含义,我希望您实际上需要这里的第二个(更复杂)选项。

<小时/>

我们将从 FieldValue.arrayUnion 开始,只是为了向您展示如何操作:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

if (user != null) {
    FirebaseFirestore db = FirebaseFirestore.getInstance(); 
    DocumentReference docRef = db.collection("day").document(user.getEmail())

    AddDayInfo memberInfo = new AddDayInfo(Type,Day,Name,Time1,Time2,Time3);
    docRef.update("messages", FieldValue.arrayUnion(memberInfo))...
<小时/>

如果您总是想将消息添加到数组的末尾,即使该消息的另一个副本已经存在,您也需要:

  1. 读取数组的当前内容
  2. 更改应用代码中的数组
  3. 将完整数组写回数据库

由于我们使用当前值来确定新值,因此最好为此使用事务:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

if (user != null) {
    FirebaseFirestore db = FirebaseFirestore.getInstance(); 
    DocumentReference docRef = db.collection("day").document(user.getEmail())

    AddDayInfo memberInfo = new AddDayInfo(Type,Day,Name,Time1,Time2,Time3);

    db.runTransaction(new Transaction.Function<Void>() {
    @Override
    public Void apply(Transaction transaction) throws FirebaseFirestoreException {
        DocumentSnapshot snapshot = transaction.get(docRef);

        List messages = snapshot.get("messages");
        messages.add(memberInfo)

        transaction.update(docRef, "messages", messages);

        // Success
        return null;
    }
})

关于java - 如何将多个数据添加到 Android Firestore 中的一个文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62360307/

相关文章:

java - 我是否必须为 Cassandra 中的每次写入打开一个新的数据库连接?

android - Sqlite - 条件选择?

ios - 无法使用 iOS Swift API 将图片上传到 Google Firebase 存储

angularjs - Firebase 简单的 REST 删除?

android - NetlinkEvent:NetlinkEvent::FindParam():未找到参数 'UID'

ios - 安装Firebase Pod时,我没有获得FirebaseDatabase模块?

java - 有人可以解释这个 maven-jar-plugin 配置吗?

java - 从错误的 xsl 格式文档中获取属性值

java - (Spark skewed join) 如何在没有内存问题的情况下连接两个具有高度重复键的大型 Spark RDD?

java - 如何在我的电脑上获取我的应用程序 (Android) 存储的文件?