简体   繁体   English

在嵌套的 map 中查询带有时间戳字段的 firestore 文档

[英]Query a firestore document with a timestamp field inside a nested map

My Documents are structured something like this -我的文件的结构是这样的 -

fieldOne = "value1"
fieldTwo = "value2"
fieldThree = "value3<Map1 = flights <Map2 = [flight_number] = <travel_date = TimeStamp>>>" 

Here the flight_number (LH760 in image below) key is dynamic and I don't know its value beforehand.这里的flight_number (下图中的 LH760)键是动态的,我事先不知道它的值。 Additionally, there could be multiple flight_number's此外,可能有多个航班号

实际数据截图

I want to find all documents where the travel_date is equal to today's date.我想找到 travel_date 等于今天日期的所有文档。 How can I most efficiently write a query (in Kotlin)?如何最有效地编写查询(在 Kotlin 中)?

I tried to filter by created (as a tutorial for myself - not as a solution for my requirement) but zero docs were returned.我尝试按created过滤(作为我自己的教程 - 不是作为我的要求的解决方案)但返回零文档。 so I cant wrap my head around how to query within such a complex nested structure -所以我不能全神贯注于如何在如此复杂的嵌套结构中进行查询 -

.whereEqualTo("created", LocalDateTime.now())

Please note: created timestamp can be different from travel_date timestamp.请注意: created时间戳可以不同于travel_date时间戳。

There is no way in the Firestore API to query in a specific field in an array or on the dynamic keys of a map.在 Firestore API 中无法查询数组中的特定字段或 map 的动态键。

What you can do is modify/augment the data model to allow your use-case.可以做的是修改/增加数据 model 以允许您的用例。 Since you want to query on travel date, add a top-level array field travel_dates where you store all the travel_date values that you also store in the flights field already in a format like "2023-01-17" (for January 17, 2023).由于您想查询旅行日期,因此添加一个顶级数组字段travel_dates ,您可以在其中存储您还存储在flights字段中的所有travel_date值,格式如"2023-01-17" (2023 年 1 月 17 日) ).

With that field in place, you can query on the new travel_dates field to get all documents for today with something like:有了该字段,您可以查询新的travel_dates字段以获取今天的所有文档,例如:

.whereArrayContains("travel_dates", "2023-01-17")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM