如何在Firestore和Android中选择位置? [英] How to select locations in Firestore and Android?

查看:94
本文介绍了如何在Firestore和Android中选择位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cloud Firestore的新手,请帮助我。我正在为青少年创建一个应用,我希望在其中显示每个城市的绝佳地理位置列表。我还希望每个用户都可以将任何位置保存到喜欢的部分,以便他或她可以将该位置列表显示给他或她的朋友。

I'm very new to Cloud Firestore, so please help me. I'm creating an app for teensagers where I want to display a list of very nice locations in every city. I also want that every user can save any location to a favorite section so he or she can show that location list to his or her friends.


我的实际数据




  • db->用户(所有用户)

  • db->位置(所有位置)


  • 我的问题:

    我想找到一种将用户的位置设置为收藏夹的方法。我正在考虑在每个用户下创建一个新部分,以保存每个喜欢的位置,例如:

    I want to find a way to set a location for a user as favorite. I'm thinking to create a new section under each user to hold every favorite location like:


    • db->用户(所有用户)- >收藏夹


    我的问题:

    如何在一个用户和喜欢的位置之间建立联系?我认为这是一对多的关系。最好的解决方案是什么?

    How to make the connection between one user and favorite location/locations? I think it is a one (user) to many relationship. What is the best solution to do this?

    任何想法或一段代码都将受到赞赏!

    Any idea or piece of code will be appreciated!

    推荐答案


    什么是最好的解决方案?

    What is the best solution to do this?

    我知道每个人都在寻找最好的解决方案,但是在NoSQL数据库领域,没有没有最佳解决方案。您可以通过多种方式为像您这样的应用程序建模数据库,并获得相同的结果,我将向您说明原因。在此之前,请查看我对此 发布 ,以更好地理解完美,最佳或正确解决方案的概念(如果出现这种情况) NoSQL数据库。

    I know that everyone is looking for the best solution but in the land of NoSQL databases, there is no best solution. There are many ways in which you can model a database for an app like yours and have the same result and and I'll demonstare you why. Before that, please take a look at my answer from this post, to have a better understanding about the concept of "perfect", "the best" or "the correct" solution in case of a NoSQL database.

    最好的解决方案是,只是在开玩笑:)一种推荐的方法可以使用<$来实现您要查找的该喜爱的功能列表。 c $ c>数组。您的数据库架构可能看起来像这样:

    The best solution is, just kidding :) A recommend way in which you can achieve this favorite feature list that you are look for, is to use arrays. Your database schema might look like this:

    Firestore-root
       |
       --- users (Collection)
       |     |
       |     --- uid (document)
       |           |
       |           --- favoriteLocations: ["favoriteLocationId", "favoriteLocationId"]
       |
       --- locations (Collection)
             |
             --- locationId (document)
                   |
                   --- //Location details
    

    如您所见, favoriteLocations 数组仅包含位置ID。因此,为了获取位置详细信息,您应该获取这些ID并进行额外的数据库 get()调用。

    As you can see, the favoriteLocations array holds only location ids. So in order to get location details, you should get those ids and make an extra database get() call.

    另一种方法是代替存储位置ID的数组,而是存储地图数组或位置对象的数组。如果您有相当数量的喜欢的位置,例如5、10甚至50,但不是 500,则此选项效果很好。如果您需要存储500个位置,那是什么原因将位置存储为收藏夹?

    Another approach would be, instead of storing an array of location ids, to store an array of maps or an array of location objects. This option works fine if you have a reasonable number of favorite locations, let's say 5, 10 or even 50 but not 500. If you'll need to store 500 location, what is the reason you'll store a location as favorite?

    在这种情况下要记住的重要一点是文档有限制。因此,在文档中可以放入多少数据方面存在一些限制。根据有关用法和限制的官方文档:

    An important thing to remember in this case, is that the documents have limits. So there are some limits when it comes to how much data you can put into a document. According to the official documentation regarding usage and limits:


    文档的最大大小:1 MiB(1,048,576字节)

    Maximum size for a document: 1 MiB (1,048,576 bytes)

    As您可以看到,单个文档中的数据总数限制为1 MiB。当我们谈论存储文本时,可以存储很多,但是随着数组越来越大,请注意此限制。

    As you can see, you are limited to 1 MiB total of data in a single document. When we are talking about storing text, you can store pretty much but as your array getts bigger, be careful about this limitation.

    如果要存储更多的数据,单个文档可以存储,那么您应该使用集合。这样,您可以在将作为文档(位置对象)保存的每个用户对象下创建一个子集合。您的数据库结构应如下所示:

    If you want to store more data that a single documents allow you to store, then you should use collections. This way you can create a subcollection under each user object that will hold as documents, location objects. Your database structure should like something like this:

    Firestore-root
       |
       --- users (Collection)
       |     |
       |     --- uid (document)
       |           |
       |           --- favoriteLocations (Collection)
       |                |
       |                --- favoriteLocationId (document)
       |                       |
       |                       --- //Location details
       |
       --- locations (Collection)
             |
             --- locationId (document)
                   |
                   --- //Location details
    

    要查询如下所示的数据库,请使用以下代码行:

    To query a database that looks like this, please use the following lines of code:

    FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
    CollectionReference favoriteLocationsRef = rootRef.collection("users").document(uid).collection(favoriteLocations);
    favoriteLocationsRef.get(/* ... */);
    

    另一种方法是对数据进行反义化并创建 favoriteLocations 子集合,例如这样的顶级集合:

    Another approach would be to denomalize your data and create your favoriteLocations subcollection, as a top-level collection like this:

    Firestore-root
       |
       --- users (Collection)
       |     |
       |     --- uid (document)
       |           |
       |           --- //User details
       |
       --- locations (Collection)
       |     |
       |     --- locationId (document)
       |           |
       |           --- //Location details
       |
       --- favoriteLocations (Collection)
             |
             --- uid (document)
                  |
                  --- userFavoriteLocations (collection)
                        |
                        --- favoriteLocationId (document)
                               |
                               --- //Location details
    

    相应的查询应如下所示:

    The corresponding query should look like this:

    FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
    CollectionReference favoriteLocationsRef = rootRef.collection("favoriteLocations").document(uid).collection(userFavoriteLocations);
    favoriteLocationsRef.get(/* ... */);
    

    或更简单的方法是:

    Firestore-root
       |
       --- users (Collection)
       |     |
       |     --- uid (document)
       |           |
       |           --- //User details
       |
       --- locations (Collection)
       |     |
       |     --- locationId (document)
       |           |
       |           --- //Location details
       |
       --- favoriteLocations (Collection)
             |
             --- locationId (document)
                   |
                   --- uid: "uid"
                   |
                   --- //Other location details
    
    FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
    Query query = rootRef.collection("favoriteLocations").whereEqualTo("uid", uid);
    favoriteLocationsRef.get(/* ... */);
    

    如您所见,我已将用户的uid添加为location对象的属性,因此您可以简单地获得特定用户拥有的所有喜欢的位置。

    As you can see, I have added the uid of the user as a property of the location object so you can simply get all favorite locations a particular user has.

    由您选择哪种解决方案对您的应用使用最佳 -case:)

    It's up to you to choose which solution is "best" for your app use-case :)

    PS 请也看看我从这个 帖子 ,我在其中详细说明了集合地图数组在Firestore中。

    P.S. Please also take a look at my answer from this post where I have explained more about collections, maps and arrays in Firestore.

    这篇关于如何在Firestore和Android中选择位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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