我想提出一個帶有過濾屬性的請求。我知道使用 firebase ,我們不能將 where 條件與多個不同的欄位一起使用,或者將 Where 與欄位屬性 A 一起使用并按屬性 B 排序。所以我使用索引進行測驗。
我為一個集合創建一個索引
D B
收藏:地理
資料示例:
{
date: 1638926138,
localisation: { latitude: 48.120599, longitude: 2.0256055 },
po: 1251653A,
st: pQN8,
user: OeS2
}
我創建一個索引:
Id collection: geo
fields index : date => descending, localisation.latitude => ascending
status : activate
要求:
import firestore from "@react-native-firebase/firestore";
firestore()
.collection(COLLECTION)
.where("localisation.latitude", ">=", lat_min)
.where("localisation.latitude", "<=", lat_max)
.get()
.then(querySnapshot => {})
它作業正常(但我想使用我的索引與欄位“日期”)
import firestore from "@react-native-firebase/firestore";
firestore()
.collection(COLLECTION)
.where("localisation.latitude", ">=", lat_min)
.where("localisation.latitude", "<=", lat_max)
.orderBy('date')
.get()
.then(querySnapshot => {})
錯誤
可能未處理的承諾拒絕(id:0):
錯誤:firebase.firestore().collection().orderBy() 查詢無效。初始 Query.orderBy() 引數:日期必須與 Query.where() fieldPath 引數相同:呼叫不等式運算子時的 localisation.latitude
感謝幫助!
解決
哦,如果我要測驗這個請求,現在謝謝
firestore()
.collection(COLLECTION)
.orderBy("localisation.latitude")
.where("localisation.latitude", ">=", lat_min)
.where("localisation.latitude", "<=", lat_max)
.orderBy('date', 'desc')
.get()
寬度索引:
localisation.latitude => ascending
date => descending
但我用 localisation.longitude 測驗
其他測驗
我的指數:
localisation.latitude => ascending
localisation.longitude => ascending
date => descending
我的請求
firestore()
.collection(COLLECTION)
.orderBy("localisation.latitude")
.where("localisation.latitude", ">=", lat_min)
.where("localisation.latitude", "<=", lat_max)
.orderBy("localisation.longitude")
.where("localisation.longitude", ">=", lon_min)
.where("localisation.longitude", "<=", lon_max)
.orderBy('date', 'desc')
這不行
錯誤
Possible Unhandled Promise Rejection (id: 0):
Error: firebase.firestore().collection().where() Invalid query. All where filters with an inequality (<, <=, >, != or >=) must be on the same field. But you have inequality filters on 'localisation.latitude' and 'localisation.longitude'
uj5u.com熱心網友回復:
正如錯誤訊息所說,您需要先訂購localisation.latitude,然后才能訂購date。
firestore()
.collection(COLLECTION)
.orderBy("localisation.latitude")
.where("localisation.latitude", ">=", lat_min)
.where("localisation.latitude", "<=", lat_max)
.orderBy('date')
您可能還需要為此特定訂單創建索引。
在orderBy查詢中使用這兩個呼叫也意味著您的結果將首先按localisation.latitude順序排列,然后按date順序排列。沒有辦法改變這一點:
如果您希望首先按日期順序顯示結果,則需要在應用程式代碼中對它們重新排序。
無關:鑒于您按緯度進行過濾,我建議您查看geoqueries上的檔案,它允許您搜索某個區域中的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/380975.html
標籤:javascript 火力基地 反应原生 谷歌云firestore
