N1QL 聚合查询 Couchbase

N1QL aggregation Query Couchbase

我正在尝试编写一个查询,该查询将针对源和目标的组合在给定星期几的文档中汇总结果。

存储桶中的文件如下所示

{
  "source": "test-source-1",
  "target": "test-target-1",
  "2022-03-05": {
    "day_of_week": "Saturday",
    "result-1": 467.5326086956522,
    "result-2": {
      "low_limit": 21.0,
      "high_limit": 14.0
    },
    "result-3": {
      "low_limit": 3.0,
      "high_limit": 2.0
    }
  },
  "2022-03-06": {
    "day_of_week": "Sunday",
    "result-1": 467.5326086956522,
    "result-2": {
      "low_limit": 21.0,
      "high_limit": 14.0
    },
    "result-3": {
      "low_limit": 3.0,
      "high_limit": 2.0
    }
  },
  "2022-03-12": {
    "day_of_week": "Saturday",
    "result-1": 467.5326086956522,
    "result-2": {
      "low_limit": 21.0,
      "high_limit": 14.0
    },
    "result-3": {
      "low_limit": 1.0,
      "high_limit": 1.0
    },
    "result-4": {
      "low_limit": 3.0,
      "high_limit": 2.0
    }
  },
  "2022-03-13": {
    "day_of_week": "Sunday",
    "result-1": 190.8181818181818,
    "result-2": {
      "low_limit": 30.0,
      "high_limit": 30.0
    },
    "result-3": {
      "low_limit": 1.0,
      "high_limit": 1.0
    },
    "result-4": {
      "low_limit": 6.0,
      "high_limit": 6.0
    }
  }
}

{
  "source": "test-source-2",
  "target": "test-target-2",
  "2022-03-05": {
    "day_of_week": "Saturday",
    "result-1": 300,
    "result-2": {
      "low_limit": 21.0,
      "high_limit": 14.0
    },
    "result-3": {
      "low_limit": 3.0,
      "high_limit": 2.0
    }
  },
  "2022-03-06": {
    "day_of_week": "Sunday",
    "result-1": 400,
    "result-2": {
      "low_limit": 21.0,
      "high_limit": 14.0
    },
    "result-3": {
      "low_limit": 3.0,
      "high_limit": 2.0
    }
  },
  "2022-03-12": {
    "day_of_week": "Saturday",
    "result-1": 300,
    "result-2": {
      "low_limit": 21.0,
      "high_limit": 14.0
    },
    "result-3": {
      "low_limit": 1.0,
      "high_limit": 1.0
    },
    "result-4": {
      "low_limit": 3.0,
      "high_limit": 2.0
    }
  },
  "2022-03-13": {
    "day_of_week": "Sunday",
    "result-1": 400,
    "result-2": {
      "low_limit": 30.0,
      "high_limit": 30.0
    },
    "result-3": {
      "low_limit": 1.0,
      "high_limit": 1.0
    },
    "result-4": {
      "low_limit": 6.0,
      "high_limit": 6.0
    }
  }
}

如果我们在所有文档中查询 where day_of_week = Saturday,预期结果应该如下:

[
    {
        "2022-03-05": {
            "day_of_week": "Saturday",
            "result-1": 467.5326086956522,
            "result-2": {
              "low_limit": 21.0,
              "high_limit": 14.0
            },
            "result-3": {
              "low_limit": 3.0,
              "high_limit": 2.0
            }
          },
        "2022-03-12": {
            "day_of_week": "Saturday",
            "result-1": 467.5326086956522,
            "result-2": {
              "low_limit": 21.0,
              "high_limit": 14.0
            },
            "result-3": {
              "low_limit": 1.0,
              "high_limit": 1.0
            },
            "result-4": {
              "low_limit": 3.0,
              "high_limit": 2.0
            }
          }
        "source": "test-source-1",
        "target": "test-target-1"
    },
    {
        "2022-03-05": {
            "day_of_week": "Saturday",
            "result-1": 300,
            "result-2": {
              "low_limit": 21.0,
              "high_limit": 14.0
            },
            "result-3": {
              "low_limit": 3.0,
              "high_limit": 2.0
            }
          },
        "2022-03-12": {
            "day_of_week": "Saturday",
            "result-1": 300,
            "result-2": {
              "low_limit": 21.0,
              "high_limit": 14.0
            },
            "result-3": {
              "low_limit": 1.0,
              "high_limit": 1.0
            },
            "result-4": {
              "low_limit": 3.0,
              "high_limit": 2.0
            }
          },
        "source": "test-source-2",
        "target": "test-target-2"
    }
]

到目前为止,我有以下查询,但它 return 全部 day_of_week。 我知道我正在选择 b.* 这将 return 所有天只是不确定如何过滤掉每一天,即只有周六或周日

SELECT b.*
FROM `history-dummy`.`_default`.`node-to-node` AS b
WHERE ANY v IN OBJECT_VALUES(b) SATISFIES v.`day_of_week`="Saturday" END;

如有任何帮助,我们将不胜感激。谢谢

SELECT RAW OBJECT n:v FOR n:v IN b WHEN v.day_of_week = "Saturday" END
FROM `history-dummy`.`_default`.`node-to-node` AS b
WHERE ANY v IN OBJECT_VALUES(b) SATISFIES v.`day_of_week`="Saturday" END;

添加源、目标

SELECT b.source, b.target, OBJECT n:v FOR n:v IN b WHEN v.day_of_week = "Saturday" END.*
FROM `history-dummy`.`_default`.`node-to-node` AS b
WHERE ANY v IN OBJECT_VALUES(b) SATISFIES v.`day_of_week`="Saturday" END;

CREATE INDEX `idx1` ON `history-dummy`.`_default `.`node-to-node`(DISTINCT ARRAY v.day_of_week FOR v IN OBJECT_VALUES(self) END);