Query Google Firestore database by custom object fields

前端 未结 2 1187
生来不讨喜
生来不讨喜 2021-01-19 17:47

I\'ve got the following Google Firestore database structure:

Is there a way to query by a range of each - latitude and longitude? For example I want to get all user

相关标签:
2条回答
  • 2021-01-19 17:55

    Maybe a solution for this is to have a cloud function that adds a lat_range and a lon_range field to each document as it comes into Firebase? You'll need to have predefined ranges though - so you know what you can query.

    0 讨论(0)
  • 2021-01-19 18:10

    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.

    0 讨论(0)
提交回复
热议问题