couchdb 中两个关键范围的问题
我在坐标系中无法获得正确的结果。 为了解释我的系统,我有一个简单的数据库,其中包含 x_axis、y_axis 和 name 列。我不需要获取所有数据,我只需要显示其中的一部分。
例如,我有一个 10:10 的坐标系(意味着从 x_axis -10 到 10 和从 y_axis -10 到 10),我只想显示 49 个坐标。在 sql 查询中我可以这样做: “从 x_axis >= -3 和 x_axis <= 3 且 y_axis >= -3 y_axis <= 3 的坐标中选择 *”
我尝试了此功能但没有成功:
"by_range": {
"map": "function(doc) { emit([doc.x_axis, doc.y_axis], doc) }"
}
by_range?startkey=[-3,-3] &endkey=[3,3]
我得到的错误结果为:
-3x-3 -3x-2 -3x-1 -3x0 -3x1 -3x2 -3x3 <-- 不应显示此部分 --> -3x4 -3x5 -3x6 -3x7 -3x8 -3x9 -3x10 <-- 结尾不应该显示这部分 --> ..... 高达 3x3,
为了让您更好地了解我的项目,这里是我想要制作的屏幕截图:
I'm having problem getting the right results in my coordinate system.
To explain my system, I have this simple database that have x_axis, y_axis and name columns. I don't need to get all the data, I just need to display some part of it.
For example, I have a coordinate system that have 10:10(meaning from x_axis -10 to 10 and from y_axis -10 to 10) and I want to display only 49 coordinates. In sql query I can do it something like this:
"select * from coordinate where x_axis >= -3 and x_axis <= 3 and y_axis >= -3 y_axis <= 3"
I tried this function but no success:
"by_range": {
"map": "function(doc) { emit([doc.x_axis, doc.y_axis], doc) }"
}
by_range?startkey=[-3,-3]&endkey=[3,3]
I got a wrong results of:
-3x-3
-3x-2
-3x-1
-3x0
-3x1
-3x2
-3x3
<-- should not display this part -->
-3x4
-3x5
-3x6
-3x7
-3x8
-3x9
-3x10
<-- end of should not display this part -->
..... up to 3x3
to give you a better understanding of my project here is the screenshot of that I want to be made:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将其用作 WHERE 子句。 Couchdb 不理解“startkey”和“endkey”中的值,它只是使用它们来知道何时开始和停止输出结果。
例如,采用以下结果集:
如果我应用此查询:
?startkey=doc2&endkey=doc3
结果集将为:
要在示例中应用范围,我将修改映射函数:
更新以处理动态范围:
根据数据库查询CouchDB方式:
因此,要实现动态范围,您需要将键值的输出更改为单个值,以便可以使用 startkey 和 endkey 参数。
使用此过滤器:
在您的应用程序中,您可以通过执行类似的操作来计算开始和结束键
You are using this like a WHERE clause. Couchdb does not understand the values in "startkey" and "endkey", it just uses them to know when to start and stop outputting results.
For example, take the following result set:
If I apply this query:
?startkey=doc2&endkey=doc3
The result set would be:
To apply a range in your example, I would modify the map function:
Update to handle dynamic ranges:
According to Database Queries the CouchDB Way:
So to do a dynamic range, you need to alter the output of the key value to a single value so you can use startkey and endkey parameters.
Use this filter:
In your application you can calculate the start and end keys by doing something like
请参阅以下页面:
基本上有2个选项:
See these pages:
Basically 2 options:
您可以执行类似具有多个键的 WHERE 子句之类的操作以及此 文章。
You can do something like a WHERE clause with multiple keys and the technique described in this article.