如何使用Kotlin更深入地使用属性查询Firestore集合? [英] How to query a Firestore collection using a property from a step deeper with Kotlin?

查看:81
本文介绍了如何使用Kotlin更深入地使用属性查询Firestore集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cloud Firestore数据库用于运输:

I have a Cloud Firestore database for transportations:

  • 交通运输{collection}
    • TransportationId {文档}
      • TransportationName:私人运输"
      • 汽车{收藏}
        • CarId {document}
          • CarName:出租车"
          • Transportations {collection}
            • TransportationId {document}
              • TransportationName: "Private Transaportation"
              • Cars {collection}
                • CarId {document}
                  • CarName: "Taxi"
                  • 汽车名称:私人出租车"

                  Cars集合中,我有100余辆汽车(文档).我在文档中或在stackoverflow上都找不到如何获取CarName名称具有特定特定值的所有运输名称的方法.该如何解决?

                  In Cars collection I have over 100 cars (documents). I cannot find in the documentation nor on stackoverflow how to get all the transportation names in which the CarName name has a specific particular value. How to solve this?

                  val Taxi = db
                      .collection("Transportations").document("TransportationId")
                      .collection("Cars").whereEqualTo("CarName", "Private Taxi")
                  

                  谢谢!

                  推荐答案

                  简短的答案是您无法在Firestore中实现这一目标.无论包含哪种交通工具,都无法在数据库中查询所有汽车.

                  The short answer is that you cannot achieve this in Firestore. You cannot query your database for all the cars regardless of which transportation contained them.

                  但是在这种情况下,有一种解决方法,您应该通过创建一个名为cars的新顶级集合来稍微更改数据库结构,并在其中添加所有car对象.在每个car对象之外,您还应该添加所需的transportation作为属性.这样,您就可以根据需要查询数据库.

                  But in such cases, there is a workaround in which you should change your database structure a little bit by creating a new top-level collection named cars in which you should add all your car objects. Beheath each car object you'll also should add the desired transportation as a property. In this way you'll be able to query the database according to your needes.

                  您的数据库结构应如下所示:

                  Your database structure should look like this:

                  Firestore-root
                     |
                     --- cars (collection)
                          |
                          --- carId (document)
                                |
                                --- transportation: "Private Taxi"
                                |
                                --- //other car details
                  

                  这篇关于如何使用Kotlin更深入地使用属性查询Firestore集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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