problems with two key ranges in couchdb

南笙酒味 提交于 2019-12-06 04:08:14
by_range?startkey=[-3,-3]&endkey=[3,3]

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

If I apply this query:

?startkey=doc2&endkey=doc3

The result set would be:

doc2
doc3

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)
}

Update to handle dynamic ranges:

According to Database Queries the CouchDB Way:

"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."

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)
}

Use this filter:

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)
andyuk

See these pages:

Basically 2 options:

  1. Use 2 seprate queries and merge the results.
  2. Use couchdb-lucene

You can do something like a WHERE clause with multiple keys and the technique described in this article.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!