本文介绍了Couch DB按键过滤并按另一个字段排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ouchdb中,我需要按键进行过滤,并且这样做是这样的。

  {
_id : _design / test,
_rev: 6-cef7048c4fadf0daa67005fefe,
language: javascript,
views:{
all: {
map: function(doc){if(doc.blogId){emit(doc.key,doc);}}
}
}
}

但是结果应该由另一个键(doc.anotherkey)排序。因此,使用相同的功能,如何通过另一个键实现过滤和排序。



谢谢

解决方案

如果只需要按单个键进行查询,则可以使用以下地图:

 函数(doc){
if(doc.blogId){
发射([doc.key,doc.anotherkey],1);
}
}

并查询 KEY 和?startkey = [ KEY]& endkey = [ KEY,{}]& include_docs = true 。

根据:




  • [ KEY] 的值小于任何 [ KEY, OTHER] 的值(因为较长的数组按其前缀排序),但大于任何 [ KEY2, OTHER] 与 KEY2< KEY ;

  • 和 [ KEY,{}] 的值大于任何 [ KEY, OTHER] 值,如果 doc.otherkey 从来不是JSON对象(因为JSON对象位于其他任何JSON值之后),但小于任何 [ KEY2, OTHER] 和 KEY2 > KEY 。



当然,这不仅限于字符串。只要排序规则正确,任何类型的值都可以使用。



记住URL,对 startkey 和 endkey 。例如,使用 curl 并假设您的数据库为 DB:

  curl'http:// localhost:5984 / DB / _design / test / _view / all?startkey =%5B%22KEY%22%5D& endkey =%5B%22KEY%22,%7B%7D%5D& include_docs = true'

请注意,我已经使用了 include_docs 查询参数,而不是使用 emit(...,doc)发出整个文档,以节省磁盘空间。查询参数记录在中。



要按降序对结果进行排序,请使用 descending = true 查询参数并交换 startkey 和 endkey 作为。

  curl'http:// localhost:5984 / DB / _design / test / _view / all?endkey =%5B%22KEY%22%5D& startkey =%5B%22KEY%22,%7B%7D%5D& include_docs = true& descending = true'


In couchdb I need to filter by key and which is done like this.

{
   "_id": "_design/test",
   "_rev": "6-cef7048c4fadf0daa67005fefe",
   "language": "javascript",
   "views": {
       "all": {
           "map": "function(doc) { if (doc.blogId) {emit(doc.key, doc);} }"
       }
   }
}

However the results should be ordered by another key (doc.anotherkey). So using the same function how do I achieve both filtering and ordering by another key.

Thank you

解决方案

If you only need to query by single key, you can use the following map:

function (doc) {
  if (doc.blogId) {
    emit([doc.key, doc.anotherkey], 1);
  }
}

and query for "KEY" with ?startkey=["KEY"]&endkey=["KEY",{}]&include_docs=true.

Here, according to the collation specification of CouchDB:

  • ["KEY"] is a value lesser than any ["KEY","OTHER"] value (because longer arrays sort after their prefixes), but greater than any ["KEY2","OTHER"] with "KEY2" < "KEY";
  • and ["KEY",{}] is a value greater than any ["KEY","OTHER"] value, if doc.otherkey is never a JSON object (because JSON objects comes after any other JSON value), but lesser than any ["KEY2","OTHER"] with "KEY2" > "KEY".

Of course this is not limited to strings. Any type of value will work, as long as the collation is right.

Remember to URL encode the values in startkey and endkey. For example, using curl and assuming your database is "DB":

curl 'http://localhost:5984/DB/_design/test/_view/all?startkey=%5B%22KEY%22%5D&endkey=%5B%22KEY%22,%7B%7D%5D&include_docs=true'

Note that I've used the include_docs query parameter, instead of emitting the entire document with emit(..., doc), to save disk space. Query parameters are documented on CouchDB documentation.

To sort results in descending order, use the descending=true query parameter and swap the values of startkey and endkey as documented in the definitive guide book.

curl 'http://localhost:5984/DB/_design/test/_view/all?endkey=%5B%22KEY%22%5D&startkey=%5B%22KEY%22,%7B%7D%5D&include_docs=true&descending=true'

这篇关于Couch DB按键过滤并按另一个字段排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:20