【问题标题】:How to sort data in Firestore according to a property?如何根据属性对 Firestore 中的数据进行排序?
【发布时间】:2019-04-22 17:38:12
【问题描述】:

我有一个充满汽车的数据库。 JSON 格式是这样的:

docId: {
        "city": "Amsterdam"
        ... other properties
   }

docId 是由add 函数生成的密钥。

我这样查询数据库:

db.collection("EU-CARS").whereEqualTo("city", "Amsterdam");

我得到了阿姆斯特丹所有可用的汽车,但我想得到这样的东西:

Amsterdam
    docId
    docId
Utrecht
    docId
    docId

每个城市都有相应的汽车。如何在不为每个城市创建查询的情况下解决这个问题?

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

    你应该使用orderBy(),见https://firebase.google.com/docs/firestore/query-data/order-limit-datahttps://firebase.google.com/docs/reference/android/com/google/firebase/firestore/Query.html#orderBy(java.lang.String)

    所以你会这样做:

    db.collection("EU-CARS").orderBy("city")
    

    你会得到以下内容

    docId1
       city: Amsterdam
       ...
    docId2
       city: Amsterdam
       ...
    docId3
       city: Utrecht
       ...
    docId4
       city: Utrecht
       ...
    

    您可以在前端将其转换为您正在寻找的确切格式。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-19
    • 2015-02-02
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多