久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Firebase 日期范圍查詢以獲取具有 starDate 和 endDa

Firebase date range query to fetch documents that have starDate and endDate(Firebase 日期范圍查詢以獲取具有 starDate 和 endDate 的文檔)
本文介紹了Firebase 日期范圍查詢以獲取具有 starDate 和 endDate 的文檔的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我的 Firestore 數(shù)據(jù)庫中有一系列任務,如下所示:

I have a collection of tasks on my Firestore database like this:

"tasks": {
    v8NC4GovVOPe21fhwtp0 : {
        id: "v8NC4GovVOPe21fhwtp0"
        status: 0
        dateFrom: 30 april 2019 at 00:00:00 UTC-3
        dateTo: 05 may 2019 at 00:00:00 UTC-3
        ....
    },
    v7NC4GaxVOPe48ftstp1 : {
        id:"v7NC4GaxVOPe48ftstp1"
        status: 0
        dateFrom: 29 april 2019 at 00:00:00 UTC-3
        dateTo: 02 may 2019 at 00:00:00 UTC-3
        ....
    }
}

文檔有一個 dateFrom 字段,表示用戶可以開始處理該任務的第一個日期和一個 dateTo 字段,表示用戶擁有的最后一天完成該任務.

The document has a dateFrom field, which indicates the first date the user can start working on that task and an dateTo field, which indicates the last day the user have to finish that task.

我試圖從 2019 年 4 月開始獲取所有任務,這意味著 dateFromdateTo 屬于本月/年的任務(在本例中,如果月份是 2019 年 4 月,它必須同時獲取這兩個任務).

I was trying to get all tasks from April 2019, which means the ones that either dateFrom or dateTo belongs to this month/year (in this example, if the month is April-2019, it must fetch both tasks).

在 Android 上,我得到了所選月份的第一個日期 (startDate) 和所選月份的最后一個日期 (endDate),我正在嘗試做某事喜歡:

On Android, I get the first date of the chosen month (startDate) and te last date from the chosen month (endDate) and I was trying to do something like:

val missionsCollection = FirebaseFirestore.getInstance()
        .collection("/users").document(uid).collection("tasks")
        .whereGreaterThanOrEqualTo("dateFrom", startDate).whereLessThanOrEqualTo("dateTo", endDate)

但我收到一條錯誤消息,提示 除 whereEqualTo() 之外的所有 where 過濾器都必須位于同一字段上.但是您在dateFrom"和dateTo"上有過濾器.

But I get an error saying that All where filters other than whereEqualTo() must be on the same field. But you have filters on 'dateFrom' and 'dateTo'.

我想了一些解決方法,但它們看起來都很糟糕,即使我找不到一個好的解決方案,我相信一定有一個.

I thought about some workarounds, but they all look bad and even though I could not find a nice solution, I'm sure there's gotta be one.

推薦答案

Firestore 允許鏈接多個where()"方法來創(chuàng)建更具體的查詢,但僅限于同一字段.正如您可能在官方文檔中看到的那樣 查詢限制:

Firestore allows to chain multiple "where()" methods to create more specific queries but only on the same field. As you can probably see in the official documentation regardin Query limitations:

Cloud Firestore 不支持以下類型的查詢:

Cloud Firestore does not support the following types of queries:

  • 在不同字段上使用范圍過濾器進行查詢,如上一節(jié)所述.

因此禁止對不同字段進行范圍過濾.

So range filters on different fields are forbidden.

為了達到你想要的,你需要查詢你的數(shù)據(jù)庫兩次,一次是使用調(diào)用來過濾數(shù)據(jù):

To achieve what you want, you need to query your database twice, once to fiter data using a call to:

.whereGreaterThanOrEqualTo("dateFrom", startDate)

第二次使用調(diào)用:

.whereLessThanOrEqualTo("dateTo", endDate)

但很遺憾,您不能在同一個查詢中使用它們.

But unfortunately you cannot use them in the same query.

當我們設計數(shù)據(jù)庫架構(gòu)時,基本上我們會為查詢構(gòu)建它.因此,除了上述解決方案(您可以過濾查詢中的一個字段以及客戶端代碼中的另一個字段)之外,還有另一種方法可以將兩個范圍的值組合到一個字段中某種方式可以讓您的用例使用單個字段.

When we are designing a database schema, basically we structure it for our queries. So beside the above solution in which you can filter on one field in the query, and on the other field in your client-side code, there is also another approach in which you can combine the values of the two range into a single field in some way that will allow your use-case with a single field.

到目前為止,我見過的最成功的例子之一是 Geohashes 中用于過濾緯度和經(jīng)度的組合,正如 Frank van Puffelen 在以下視頻中所解釋的那樣:

One of the most successful examples I've seen so far is the combination used in Geohashes to filter on latitude and longitude as explained by Frank van Puffelen in the following video:

  • https://www.youtube.com/watch?v=mx1mMdHBi5Q

知道這兩種解決方案在工作量上的區(qū)別,我建議使用第一種.

Knowing the difference in effort between these two solutions, I'd recommend using the first one.

還有第三種解決方案,您應該將 3 月至 4 月的所有任務添加到一個集合中.然后您可以使用以下命令查詢這個新集合:

There is also a third solution, in which you should to add all tasks from Mar-Apr into a single collection. Then you could query this new collection with:

 db.collection("tasks-aps-mar")
    .whereGreaterThanOrEqualTo("day", 1)
    .whereLessThanOrEqualTo("day", 30);

更通用的解決方案是將每個月的任務存儲在一個集合中,然后執(zhí)行查詢以獲取與所需月份對應的所有任務.在您的用例中,您應該查詢數(shù)據(jù)庫以獲取每個集合 tasks-mar-2019tasks-apr-2019tasks- 之一中的文檔2019 年 5 月 等等.

Even a more general solution would be to store the tasks in a collection for each month, and then perform a query to get all the tasks that correspond with the desired month. In your use case, you should query your database to get the documents within one of each collection tasks-mar-2019, tasks-apr-2019, tasks-may-2019 and so on.

關(guān)于您的評論,使用數(shù)組對您毫無幫助,因為您不能使用范圍間隔.

Regarding your comment, using arrays won't help you at all since you cannot use range intervals.

這篇關(guān)于Firebase 日期范圍查詢以獲取具有 starDate 和 endDate 的文檔的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

IncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出現(xiàn) IncompatibleClassChangeError)
How to get current flavor in gradle(如何在 gradle 中獲取當前風味)
How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修復“意外元素lt;查詢gt;在“清單中找到錯誤?)
Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多風味庫的多風味應用)
Android dependency has different version for the compile and runtime(Android 依賴在編譯和運行時有不同的版本)
Transitive dependencies for local aar library(本地 aar 庫的傳遞依賴)
主站蜘蛛池模板: 99精品一区二区 | 国产免费一区二区三区 | 中文字幕二区三区 | 国产精品精品视频一区二区三区 | 国产精品无码久久久久 | 欧美成人精品一区二区三区 | 亚洲精品久久久久久久不卡四虎 | 91麻豆精品国产91久久久久久 | 91色视频在线观看 | 理论片免费在线观看 | 91色在线视频 | 亚洲电影一区二区三区 | 国产一级在线观看 | 黄色大片免费网站 | 精品欧美一区二区三区精品久久 | 在线观看视频一区二区三区 | 国产午夜精品一区二区三区四区 | 国产女人与拘做受视频 | 国产一区不卡 | 人人擦人人干 | 日韩欧美在线观看视频 | 在线看av网址 | 自拍偷拍一区二区三区 | 色屁屁在线观看 | 亚洲狠狠 | 久久777| 91精品国产乱码久久久久久久久 | 国产日产精品一区二区三区四区 | 亚洲v日韩v综合v精品v | 97久久精品午夜一区二区 | 精品久久精品 | 9191成人精品久久 | 亚洲精品久久久一区二区三区 | 欧美综合久久 | 婷婷在线免费 | 中文字幕一区在线观看视频 | 亚洲精品在线免费观看视频 | аⅴ资源新版在线天堂 | 欧美aⅴ在线观看 | 国产一区二区三区久久久久久久久 | 小视频你懂得 |