这是一个常见的误解,使用复合数组索引键,它仍然被视为字符串,因此索引键 [2,10] 实际上是“[2,10]”,而索引键 [5,20] 是实际上是“[5,20]”。
所以startkey=["2", "30"]
显示该{"id":"5","key":["5","20"],"value":null},
行的原因是因为作为一个字符串它是> startkey。
同样,查询startkey=[2,10]&endkey=[5,10]
返回
{"total_rows":7,"rows":[
{"id":"2","key":[2,20],"value":null},
{"id":"3","key":[3,30],"value":null},
{"id":"4","key":[4,30],"value":null}
]
}
因为startkey="[2,10]"
< "[2,20]" && "[4,30]" < "[5,10]"=endkey
,但 "[5,20]" 不在该字符串范围内。
使用 startkey 和 endkey 进行范围查询
startkey => endkey是使用 strcmp() 的 Range 查询,组和组级别基于字符串,其中逗号分隔字符串标记。
一个很好的参考链接(因为 Couchbase 视图的工作方式很像 Apache CouchDB 视图(受它们启发))
http://wiki.apache.org/couchdb/View_collat ion#Collation_Specification
空间视图/查询
为了达到您想要的结果,您还可以编写一个空间视图来进行多维查询,仅限数字。虽然你一开始可能不会想到
function (doc, meta) {
emit({
type: "Point",
coordinates: [doc.ID, doc.Price]
}, meta.id);
}
查询将是一个边界框查询:
&bbox=2,0,8,30
{"total_rows":0,"rows":[
{"id":"2","bbox":[2,20,2,20],"geometry":{"type":"Point","coordinates":[2,20]},"value":"2"},
{"id":"3","bbox":[3,30,3,30],"geometry":{"type":"Point","coordinates":[3,30]},"value":"3"},
{"id":"4","bbox":[4,30,4,30],"geometry":{"type":"Point","coordinates":[4,30]},"value":"4"},
{"id":"5","bbox":[5,20,5,20],"geometry":{"type":"Point","coordinates":[5,20]},"value":"5"}
]
}
另一个查询:
&bbox=2,30,8,30
{"total_rows":0,"rows":[
{"id":"3","bbox":[3,30,3,30],"geometry":{"type":"Point","coordinates":[3,30]},"value":"3"},
{"id":"4","bbox":[4,30,4,30],"geometry":{"type":"Point","coordinates":[4,30]},"value":"4"}
]
}