I would like to make a request with a property filtered. I know with firebase , we can't use the where condition with multiple differents fields Or use Where with a field property A and order by property B. So i test with the Index.
I create an index for a collection
DB
collection : geo
Example of data :
{
date: 1638926138,
localisation: { latitude: 48.120599, longitude: 2.0256055 },
po: 1251653A,
st: pQN8,
user: OeS2
}
I create an index:
Id collection: geo
fields index : date => descending, localisation.latitude => ascending
status : activate
Request:
import firestore from "@react-native-firebase/firestore";
firestore()
.collection(COLLECTION)
.where("localisation.latitude", ">=", lat_min)
.where("localisation.latitude", "<=", lat_max)
.get()
.then(querySnapshot => {})
It's work ok ( but i would like to use my index with the field 'date')
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 => {})
Error
Possible Unhandled Promise Rejection (id: 0):
Error: firebase.firestore().collection().orderBy() Invalid query. Initial Query.orderBy() parameter: date has to be the same as the Query.where() fieldPath parameter(s): localisation.latitude when an inequality operator is invoked
Thanks for help!
Resolve
oh it's work thanks just for now if i would test with this request
firestore()
.collection(COLLECTION)
.orderBy("localisation.latitude")
.where("localisation.latitude", ">=", lat_min)
.where("localisation.latitude", "<=", lat_max)
.orderBy('date', 'desc')
.get()
width the index :
localisation.latitude => ascending
date => descending
But I test with localisation.longitude
Other Test
My index :
localisation.latitude => ascending
localisation.longitude => ascending
date => descending
my request
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')
It's not work
Error
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'
CodePudding user response:
As the error message says, you'll need to order on localisation.latitude
first, and only then on date
.
firestore()
.collection(COLLECTION)
.orderBy("localisation.latitude")
.where("localisation.latitude", ">=", lat_min)
.where("localisation.latitude", "<=", lat_max)
.orderBy('date')
You may need to create an index for this specific order too.
Having these two orderBy
calls in the query also means that your results will be in localisation.latitude
order first, and in date
order second. There is no way to change this:
If you want the results in date order first, you will need to reorder them in your application code.
Unrelated: given that you're filtering by latitude, I recommend checking out the documentation on geoqueries, which allow you to search for documents in an area.