Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Google Firestore database by custom object fields

I've got the following Google Firestore database structure: enter image description here

Is there a way to query by a range of each - latitude and longitude? For example I want to get all users which have location.lat and location.lng in the range of: 51.400 to 52.000 and -0.100 to -0.300. Can anyone please help me figure out the query needed to do that?

I hope this question makes any sense. Any help appreciated.

Edit: I am not using a 'Geographical point' data type. I am using a custom object with 2 float numbers and want to do a simple number compare. The thing is that I do not know (and cannot find) a way how to build my query.

like image 691
Paul Strupeikis Avatar asked Dec 13 '22 19:12

Paul Strupeikis


1 Answers

This much should be possible

userRef.where('location.lat', '>=', 51.400).where('location.lat', '<=', 52.000);

Though I don't think you can chain more .where on it if it's not on the same field.

You could try to do a similar query for location.lng beneath it and then compare and match the users from both queries. Or check if the location.lng is in the desired range in your code and filter out those users. In any case you'll end up getting more results from the database than preferred and then discarding some.

like image 169
Bart Avatar answered Apr 02 '23 13:04

Bart