【问题标题】:storing arrays into object datatype into firestore in android and iOS(swift)在android和iOS(swift)中将数组存储到对象数据类型中的firestore中
【发布时间】:2018-02-13 04:21:59
【问题描述】:

您好,我有 2 个数组,例如 电话号码[1,2,3] 金额[10,20,30] 我需要将数据存储在单个文档中,例如 字段1:1 字段2:10

我试过的代码是

我拿了像 **

public class Pojo {
    String invitee;
    Map<Integer,Integer>phoneAmount;
    public Pojo(String invitee, Map<Integer, Integer> phoneAmount)
    {
      this.invitee = invitee;
        this.phoneAmount=phoneAmount;
    }
}**

在另一个班级

 Map<int[], int[]> phoneAmount = new HashMap<>();
               phoneAmount.put(phonenumber,amount);


db.collection("Split").document("invitees").set(phoneAmount).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.d(TAG, "DocumentSnapshot successfully written!");
                }
            })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.w(TAG, "Error writing document", e);
                        }
                    });

它显示如下错误 java.lang.RuntimeException: 无法序列化对象。不支持具有非字符串键的映射 在 com.google.android.gms.internal.zzejw.zza(未知来源) 在 com.google.android.gms.internal.zzejw.zza(未知来源) 在 com.google.android.gms.internal.zzejw.zzbp(未知来源)

【问题讨论】:

  • 尝试使用带有一个 String field 和一个 Map&lt;int,int&gt; phoneAmount 和一个构造函数 public YourPojoClass(String title, Map&lt;int,int&gt; phoneAmount ) 的 pojo 类。通过这种方式,您可以将项目添加到 phoneAmount 对象,如 phoneAmount.put(phoneNumber, amount);,然后在创建 PojoClass 的对象(如 MyPojo pojo= new MyPojo ("invite", phoneAmount );)后将文档添加到集合引用(如 db.collection("Split").document("lahari").set(yourPojoClass)
  • @MohammedFarhan 你能给我看一个这个场景的例子吗?我处于混乱状态
  • 请按照下面的亚历克斯回答,它类似于我的评论,将满足您的要求。
  • 谢谢@MohammedFarhan
  • 查看这里query

标签: android ios google-cloud-firestore


【解决方案1】:

来自official documentation

虽然 Cloud Firestore 可以存储数组,但它不支持查询数组成员或更新单个数组元素。

我个人不推荐使用这种方法。 Firebase 还建议不要使用数组的众多原因之一是它使安全规则无法编写。

但是,您仍然可以利用 Cloud Firestore 的其他功能对此类数据进行建模。

让我们举个例子。假设您有一个如下所示的数据库:

{
    title: "My Book",
    categories: [
        "science",
        "computer science",
        "technology"
    ]
}

您需要知道,如果您要查询属于“科学”类别的所有books,使用此数据结构是无法执行此查询的。

为了解决这个问题,您可以考虑另一种数据结构,其中每个类别都是地图中的键,所有值都是true

{
    title: "My Book",
    categories: {
        "science": true,
        "computer science": true,
        "technology": true
    }
}

要查询您的数据库,您可以使用以下查询:

db.collection("books")
    .whereEqualTo("categories.science", true)
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {/* ... */});

对于您的特定情况,您可以使用如下所示的数据库结构:

{
    title: "phonenumber",
    categories: {
        "12345": true,
        "67890": true,
        "43215": true
    }
}

查询请使用以下代码:

db.collection("phonenumbers")
    .whereEqualTo("categories.12345", true)
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {/* ... */});

2018 年 8 月 13 日编辑:

根据有关array membership 的更新文档,现在可以使用whereArrayContains() 方法根据数组值过滤数据。一个简单的例子是:

CollectionReference citiesRef = db.collection("cities");
citiesRef.whereArrayContains("regions", "west_coast");

此查询返回每个城市文档,其中区域字段是包含 west_coast 的数组。如果数组有多个您查询的值的实例,则该文档仅包含在结果中一次。

【讨论】:

  • 我需要查询电话号码
  • @Alex Mamo,你好。这一定是OP的答案。请给OP POJO类的结构。
  • op 和 pojo 类是什么意思
  • 请看我更新的答案。 OP 是 Original PosterPOJO classPlain Old Java Object
  • kk 我知道了如何添加到数据库
猜你喜欢
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多