我正在尝试创建一个视图,该视图返回带有文档价格变化的名称。
"name": "USD"
"price_changes": {
"0min": 0,
"15min": 0,
"30min": 0,
"60min": 0,
"90min": 0,
"120min": 0,
"150min": 0,
"180min": 0,
"210min": 0,
"240min": 0,
"270min": 0,
...
我想要的是一个返回“price_changes”中所有数据的视图。
{"id":"c95718ebd00b13bc9a8b03a0e5005d51",
"key": {"0min":0,"15min":0,"30min":0,"60min":0,"90min":0,"120min":0,"150min":0,...}
{"id":"c95718ebd00b13bc9a8b03a0e5006906",
"key": {"0min":0,"15min":0,"30min":0,"60min":0,"90min":0,"120min":0,"150min":0,...}
...
返回一组行的视图,例如每个名称只有“0mins”,“15mins”和“30mins”。
像:
"id":"c95718ebd00b13bc9a8b03a0e5005d51",
"name": "USD",
"key": {"0min":0,"15min":0,"30min":0}
单独地
"id":"c95718ebd00b13bc9a8b03a0e5006906",
"name": "GBP",
"key": {"0min":0,"15min":0,"30min":0}
到目前为止我所拥有的是(但不会返回我想要的内容):
function (doc) {
var mins, value;
if (doc.price_changes) {
for (mins in doc.price_changes) {
value = doc.price_changes[mins];
emit(doc.id, [value], mins);
}
}
}
任何想法的人?我真的找不到像这样返回数据的任何内容。
答案 0 :(得分:1)
首先,在您的视图地图功能中,您发出三件事:
emit(doc.id, [value], mins);
这不应该是什么。 As documented,你需要发出键/值对(当然,键或值可以是很多东西的数组):
Map函数接受单个文档作为参数和 (可选)发出存储在视图中的()键/值对。
根据你的说法:
我想要的是一个视图,它返回" price_changes"中的所有数据。
我的理解是你需要以下地图功能:
function (doc) {
var mins, value;
if (doc.price_changes) {
for (mins in doc.price_changes) {
value = doc.price_changes[mins];
emit(mins, [value, doc._id]);
}
}
}
它返回:
$ curl -k -X GET https://admin:****@192.168.1.106:6984/sample-repl/_design/price/_view/price
{"total_rows":11,"offset":0,"rows":[
{"id":"so_q","key":"0min","value":[0,"so_q"]},
{"id":"so_q","key":"120min","value":[0,"so_q"]},
{"id":"so_q","key":"150min","value":[0,"so_q"]},
{"id":"so_q","key":"15min","value":[0,"so_q"]},
{"id":"so_q","key":"180min","value":[0,"so_q"]},
{"id":"so_q","key":"210min","value":[0,"so_q"]},
{"id":"so_q","key":"240min","value":[0,"so_q"]},
{"id":"so_q","key":"270min","value":[0,"so_q"]},
{"id":"so_q","key":"30min","value":[0,"so_q"]},
{"id":"so_q","key":"60min","value":[0,"so_q"]},
{"id":"so_q","key":"90min","value":[0,"so_q"]}
]}
我不确定上述结果是否符合您的要求,如果我错了,请告诉我。
谢谢!只是想知道我将如何返回说前三个(如:0分钟, 15分钟,30分钟)
要获得前3个,您可以使用limit
这样的查询参数,如您所见,它将返回前3个:
$ curl -k -X GET https://admin:****@192.168.1.106:6984/sample-repl/_design/price/_view/price?limit=3
{"total_rows":11,"offset":0,"rows":[
{"id":"so_q","key":"0min","value":[0,"so_q"]},
{"id":"so_q","key":"120min","value":[0,"so_q"]},
{"id":"so_q","key":"150min","value":[0,"so_q"]}
]}
一般情况下,您可以使用query parameters来获取您想要的视图/索引。