首页 > 解决方案 > 使用 Rest Call 查询 Firebase

问题描述

我正在使用查询 https://firestore.googleapis.com/v1/projects/myproject/databases/(default)/documents/mycollection查询 Firestore

我正在关注 Json。有人可以帮助我如何根据 Rate 字段过滤我的查询。我正在编写以下查询,但它不起作用

https://firestore.googleapis.com/v1/projects/myproject/databases/(默认)/documents/mycolletion?Rate="15"

{
  "documents": [
    {
      "name": "0C45nDuozgQDOwEx5xHR",
      "fields": {
        "Clinic": {
          "stringValue": "American Hospital"
        },
        "Rate": {
          "stringValue": "140"
        },
     ,`enter code here`
      "createTime": "2020-06-28T20:32:18.776123Z",
      "updateTime": "2020-07-22T21:19:24.061647Z"
    },
    
    {
      "name": "Jm3tNVWmk4Q1pk87KL1m",
      "fields": {
        "Clinic": {
          "stringValue": "Cleaveland clinic"
        },
      "Rate": {
          "stringValue": "150"
        },
      "createTime": "2020-06-28T20:28:03.726819Z",
      "updateTime": "2020-07-22T21:19:05.073019Z"
    }
 }

标签: firebaserestgoogle-cloud-firestore

解决方案


问题是该Rate字段是一个对象(在另一个对象中使情况变得更糟),因此为了实现这一点,您需要更新 Firestore 结构以在请求 URL 中完成所有操作,或者您必须使用请求正文中的结构化查询。

  • 改变结构:

为了处理您已有的请求,您需要将结构更改为(以第一个文档为例):

{
  "name": "0C45nDuozgQDOwEx5xHR",
  "Clinic": "American Hospital",
  "Rate": "140",
  "createTime": "2020-06-28T20:32:18.776123Z",
  "updateTime": "2020-07-22T21:19:24.061647Z"
}

尽管我没有全貌,但我认为这使它更简单

  • 在您的请求正文中有一个结构化查询:

为了保持你已经拥有的结构,你需要使用这个 URL:

https://firestore.googleapis.com/v1/projects/myproject/databases/(default)/documents/:runQuery

并将其添加到请求的正文中:

"structuredQuery": {
    "from": [{
        "collectionId": "mycollection"
    }],
    "where": {
        "fieldFilter": {
            "fields": {
                "Rate": {
                    "stringValue": "15"
                }
            }
        }
    }
}

您可以通过查看runQuery 文档结构化查询文档来查看更多详细信息,以了解您可以使用这些选项做些什么。


推荐阅读