如何在具有未知字段的子对象数组上使用whereToEqual [英] How to use whereToEqual on sub object array with unknown field

查看:29
本文介绍了如何在具有未知字段的子对象数组上使用whereToEqual的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我为您展示Firestore文档结构.

Let me show you the Firestore document structure.

如何比较 stops 数组值?因为出发时间每个条目的 12:00 PM 时间都会有所不同.

How I can compare the stops array values? because time 12:00 PM will be different for each entry in the departure.

所以实际上我想获取 buss 的值,其中我的给定值与 stops 数组的任何值匹配.

So actually I want to get the value of buss where the my given value match to any value of the stops array.

推荐答案

根据@Frank van Puffelen的回答,由于Firestore不支持查询数组成员,因此您应该考虑稍微更改数据库结构.因此,应该使用 maps 而不是使用数组.因此,在每个文档中,您需要添加一个如下所示的 map :

According to @Frank van Puffelen's answer, because Firestore does not support querying array members, you should consider change your database structure a little bit. So instead of using arrays you should use maps. So inside each document you need to add a map that will look like this:

Firestore-root
   |
   --- buses (collection)
        |
        --- busId (document)
              |
              --- buss: "/buss/OdGpiY..."
              |
              --- stops (map)
                    |
                    --- IQdng...526MpZ: true
                    |
                    --- pnwJ...4P3ef: true

如您所见,我在公共汽车文档中添加了一个名为 stops 的新地图,其中包含键,站名和布尔值 true .使用这种数据库结构,您可以根据地图中存在的元素来查询数据库,如下所示:

As you can see, I have add a new map named stops inside the bus document that contains as key, the name of the station and as a value the boolean true. Using this database structure you can query your database based on elements that exist inside the map like this:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference busesRef = rootRef.collection("buses");
Query query = busesRef.whereEqualTo("stops.IQdng...526MpZ", true);

这篇关于如何在具有未知字段的子对象数组上使用whereToEqual的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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