How to use whereToEqual on sub object array with u

2019-08-27 17:50发布

Let me show you the Firestore document structure.

enter image description here

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

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

2条回答
放我归山
2楼-- · 2019-08-27 18:15

From the Firestore documentation on arrays:

Although Cloud Firestore can store arrays, it does not support querying array members or updating single array elements.

To allow querying for routes that contain a certain stop, you should store an additional field in the document that contains the stops as a set:

stops
  IQdng...526MpZ: true

Now you can query for routes with that stop by doing:

routesRef.whereEqualTo("stops.IQdng...526MpZ", true)

You'll still need your original structure for the stops too, since that's the only way to know the order of the stops and the time of each stop.

查看更多
看我几分像从前
3楼-- · 2019-08-27 18:22

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

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);
查看更多
登录 后发表回答