沙发上两个键范围的问题 [英] problems with two key ranges in couchdb

查看:68
本文介绍了沙发上两个键范围的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在坐标系中获得正确的结果. 为了解释我的系统,我有一个简单的数据库,其中包含x_axis,y_axis和name列.我不需要获取所有数据,只需要显示其中的一部分即可.

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.

例如,我有一个坐标系统,该系统具有10:10(表示从x_axis -10到10,从y_axis -10到10),并且我只想显示49个坐标.在SQL查询中,我可以做这样的事情: 从x_axis> = -3和x_axis&=; = 3且y_axis> = -3 y_axis< = 3的坐标中选择*"

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]

by_range?startkey=[-3,-3]&endkey=[3,3]

我得到了一个错误的结果:

I got a wrong results of:

-3x-3 -3x-2 -3x-1 -3x0 -3x1 -3x2 -3x3 <-不应显示此部分-> -3x4 -3x5 -3x6 -3x7 -3x8 -3x9 -3x10 <-的结尾不应显示此部分-> .....最多3x3

-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:

推荐答案

by_range?startkey=[-3,-3]&endkey=[3,3]

您正在像WHERE子句一样使用它. 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:

doc1
doc2
doc3
doc4

如果我应用此查询:

?startkey = doc2& endkey = doc3

?startkey=doc2&endkey=doc3

结果集将是:

doc2
doc3

要在您的示例中应用范围,我将修改map函数:

To apply a range in your example, I would modify the map function:

function(doc) {
 if (doc.x <= 3 && doc.x >= -3 && doc.y <= 3 && doc.y >= -3)
   emit([doc.x, doc.y], doc)
}

更新为处理动态范围:

根据数据库查询CouchDB方式:

"CouchDB设计在大型数据集上为您提供了出色的性能.但是,这意味着您无法在运行查询时将动态参数传递给地图函数."

"The CouchDB design gets you great performance on large data sets. But it means that you cannot pass dynamic parameters to your map function when you run a query."

因此,要进行动态范围调整,您需要将键值的输出更改为单个值,以便可以使用startkey和endkey参数.

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.

function(doc) {
 emit(doc.x * doc.y, doc)
}

使用此过滤器:

by_range?startkey=-9&endkey=9

在您的应用程序中,您可以通过执行类似的操作来计算开始键和结束键

In your application you can calculate the start and end keys by doing something like

startkey = (x1*y1)
endkey = (x2*y2)

这篇关于沙发上两个键范围的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆