简体   繁体   English

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

[英]Firestore read documents limitation for sorting by distance

I'm creating an app in React Native that shows restaurants and businesses near you.我正在用 React Native 创建一个应用程序,显示你附近的餐馆和企业。 I started creating it on the Firebase Realtime Database but I'm quite limited there and that's why I decided for Firestore.我开始在 Firebase 实时数据库上创建它,但我在那里非常有限,这就是我决定使用 Firestore 的原因。 But currently I have read the terms of payment and now.但目前我已经阅读了付款条件。

My problem is that I always show restaurants only in my area.我的问题是我总是只显示我所在地区的餐馆。 I have more than 2,000 companies in the database.我的数据库中有 2,000 多家公司。 But I have the order by distance in the FlatList.但我在 FlatList 中有按距离排序。 So every time I load a list, all the 2,000 documents are always retrieved from the database, and overall, the number of document readings and therefore the amount I will pay is increasing very fast.因此,每次我加载一个列表时,总是从数据库中检索所有 2,000 份文档,总体而言,文档阅读量和因此我支付的金额增长非常快。 The same applies to loading all restaurants on the map. Do you know how to do this and always limit only 20 for display but to make the distance sorting work?这同样适用于加载 map 上的所有餐厅。你知道如何做到这一点并且总是限制只显示 20 个但要使距离排序有效吗?

Now I am sorting in code.现在我在代码中排序。 Is there any alternative for limit read documents?有没有其他方法可以限制阅读文件? This code reads all more than 2000.这段代码读取的都是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. Can you filters: by distance, category, ...你能过滤:按距离,类别,......
  2. Can you use cursor so that your results will be paged: https://firebase.google.com/docs/firestore/query-data/query-cursors您可以使用 cursor 以便对结果进行分页: https://firebase.google.com/docs/firestore/query-data/query-cursors
  3. Limit query results to 30: https://firebase.google.com/docs/firestore/query-data/order-limit-data将查询结果限制为 30: https://firebase.google.com/docs/firestore/query-data/order-limit-data

If you implement geohashing, it is possible to get documents in a certain geographical range.如果你实施地理散列,就有可能获得某个地理范围内的文件。 This is explained in the Firebase documentation onimplementing geoqueries . Firebase 文档中有关实现地理查询的内容对此进行了解释。

This still won't allow you to get the N documents nearest to a certain location though.不过,这仍然不允许您获取离某个位置最近的 N 个文档。 That type of query simply isn't possible on Firestore, as it'd require the database itself to do the distance calculation, which it doesn't support.这种类型的查询在 Firestore 上根本不可能,因为它需要数据库本身进行距离计算,而它不支持。

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

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