我正在尝试编写一个查询,该查询将针对源和目标的组合在给定的一周中的某一天汇总文档的结果。
桶中的文件是这样的
{
"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"
}
]
到目前为止,我有以下查询,但它返回所有 day_of_week。 我知道我正在选择 b.* 它将返回所有天只是不确定如何为每一天过滤掉它,即只有周六或周日
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);
关于json - N1QL 聚合查询 Couchbase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71376422/