繁体   English   中英

Firestore 读取文档按距离排序的限制

[英]Firestore read documents limitation for sorting by distance

我正在用 React Native 创建一个应用程序,显示你附近的餐馆和企业。 我开始在 Firebase 实时数据库上创建它,但我在那里非常有限,这就是我决定使用 Firestore 的原因。 但目前我已经阅读了付款条件。

我的问题是我总是只显示我所在地区的餐馆。 我的数据库中有 2,000 多家公司。 但我在 FlatList 中有按距离排序。 因此,每次我加载一个列表时,总是从数据库中检索所有 2,000 份文档,总体而言,文档阅读量和因此我支付的金额增长非常快。 这同样适用于加载 map 上的所有餐厅。你知道如何做到这一点并且总是限制只显示 20 个但要使距离排序有效吗?

现在我在代码中排序。 有没有其他方法可以限制阅读文件? 这段代码读取的都是2000多条。

const Restaurants = () => {
  const [restaurants, setRestaurants] = useState([]);
  const fetchRestaurants = async () => {
    const querySnapshot = await getDocs(collection(db, "restaurants"));
    querySnapshot.forEach((doc) => {
      setRestaurants({
          name: doc.data().name,
          lat : doc.data().lat,
          lng : doc.data().lng,
      })
    });
  };
  useEffect(() => {
    fetchRestaurants();
  }, []);
  return (
    <View style={{ marginTop:18 }}>
      <FlatList
        data={restaurants.sort((a,b) => {
          const aDist = Fce.getDistance(+a.lat,+a.lng)
          const bDist = Fce.getDistance(+b.lat,+b.lng)
          return aDist - bDist;
        })}
        keyExtractor={(item)=>item.id}
        renderItem={({ item }) => (
          <View style={{ marginRight:13 }}>
            <Text>Data</Text>
          </View>
        )}
      />
    </View>
  );
}
  1. 你能过滤:按距离,类别,......
  2. 您可以使用 cursor 以便对结果进行分页: https://firebase.google.com/docs/firestore/query-data/query-cursors
  3. 将查询结果限制为 30: https://firebase.google.com/docs/firestore/query-data/order-limit-data

如果你实施地理散列,就有可能获得某个地理范围内的文件。 Firebase 文档中有关实现地理查询的内容对此进行了解释。

不过,这仍然不允许您获取离某个位置最近的 N 个文档。 这种类型的查询在 Firestore 上根本不可能,因为它需要数据库本身进行距离计算,而它不支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM