如何使用功能过滤Firestore收集数据(用于位置距离过滤) [英] How to filter Firestore collection data using function (for location distance filtering)

查看:33
本文介绍了如何使用功能过滤Firestore收集数据(用于位置距离过滤)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道如何使用一个函数(其中一些函数args是集合文档的值)来过滤文档的Firestore集合.

Would like to know how to filter a Firestore collection of docs using a function where some of the function args are the values of the collection documents.

假设某些Firestore收集了以下格式的文档

Suppose had some Firestore collection of documents of the form

{
  pointOfInterest: "Some string label"
  longitude: -100.123
  latitude: 50.456
}

也有一些代码可以检索用户的地理坐标(在我的情况下是通过react-native),类似

Also have some code that retrieves a user's geocoordinates (in my case, via react-native), so something like

const getCurrentLatLong = () => {
    // do some stuff, then...
    return { latitude: someNumber, longitude: someOtherNumber }
}

最终想要做的是运行类似的东西

What would ultimately like to be able to do is run something like

let currentLocation = getCurrentLatLong()
let filteredSet = firebase
    .firestore()
    .collection('MyCollection')
    // filtering each individual document 
    .filter(function (document, currentLocation) {
         let docLat = document.latitude
         let docLong = document.longitude
         return distance(
             {latitude: docLat, longitude: docLong},
             currentLocation) 
             < SOME_CONST_DISTANCE
     })

所以最后得到的是集合中所有文档的filteredSet,这些文档与currentLocation的距离小于SOME_CONST_DISTANCE.

So that end up with some filteredSet of all the documents in the collection that have some distance from the currentLocation less than SOME_CONST_DISTANCE.

一些谷歌搜索导致了一些潜在的起点( https://stackoverflow.com/a/45359952/8236733 https://firebase.google.com/docs/reference/js/firebase.database.Query#on ),但不确定如何使用链接中显示的内容.任何有关如何实现这一目标的建议或文档,将不胜感激.

Some googling led to some potential starting points (https://stackoverflow.com/a/45359952/8236733 and https://firebase.google.com/docs/reference/js/firebase.database.Query#on), but not quite sure how to use what's being shown in the links. Any advice or docs on how to achieve this would be appreciated.

推荐答案

无法将函数传递到Cloud Firestore中,然后该函数用来过滤返回的文档.您要么需要传递静态值(例如要返回的经度和纬度),要么需要阅读所有文档并使用客户端功能进行过滤.

There is no way to pass a function into Cloud Firestore that it then uses to filter the documents that it returns. You either need to pass in static values (e.g. the latitude and longitude you want back), or you will need to read all documents and filter with the function client-side.

您要尝试的操作被称为地理查询:根据文档到已知点的距离返回文档.不幸的是,该功能尚未内置到Cloud Firestore中,即使它支持

What you're trying to do is known as a geoquery: returning documents based on their distance to a known point. Unfortunately that functionality is not built into Cloud Firestore yet, even though it has support for a geographical point data type. It is possible to build it yourself, though you should realize such queries are quite inefficient at the moment.

要了解有关此的更多信息:

To learn more about this:

这篇关于如何使用功能过滤Firestore收集数据(用于位置距离过滤)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆