【问题标题】:Query Firebase using Rest Call使用 Rest Call 查询 Firebase
【发布时间】:2020-09-23 03:11:25
【问题描述】:

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

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

https://firestore.googleapis.com/v1/projects/myproject/databases/(default)/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"
    }
 }

【问题讨论】:

标签: firebase rest google-cloud-firestore


【解决方案1】:

问题在于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 DocumentationstructuredQuery Documentation 来查看更多详细信息,了解您还可以使用这些选项做什么。

【讨论】:

    猜你喜欢
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 2018-05-08
    • 2020-06-14
    • 2022-12-02
    • 1970-01-01
    相关资源
    最近更新 更多