以下调用匹配结果集会返回一个不以doc id a4a6cf44-8a82-494a-a2b9-f6a3ec629f17开头的结果。由于下面的结果集显示前3个键是相同的,但startkey_docid无效。
围绕这个问题只有几个问题
查看:
function (doc, meta)
{
if(meta.type == "json" && doc.type == "POST")
{
emit([doc.category, dateToArray(doc.created), doc.visibility], null);
}
}
呼叫:
?startkey=["auto",[2013,10,10,23,12,0],"EVERYONE"]&endkey=["auto",[2013,12,11,23,12,0],"EVERYONE"]&startkey_docid=a4a6cf44-8a82-494a-a2b9-f6a3ec629f17
结果:
{
total_rows: 20,
rows: [{
id: "a4a6cf44-8a82",
key: ["auto", [2013, 11, 8, 1, 17, 46], "EVERYONE"],
value: null
}, {
id: "a4a6cf44-8a82-494a-a2b9",
key: ["auto", [2013, 11, 8, 1, 17, 46], "EVERYONE"],
value: null
}, {
id: "a4a6cf44-8a82-494a-a2b9-f6a3ec629f17",
key: ["auto", [2013, 11, 8, 1, 17, 46], "EVERYONE"],
value: null
}, {
id: "41070cfc-a85c-424c-9b87-fce0616c77c1",
key: ["auto", [
2013, 11, 11, 20, 28, 21], "EVERYONE"],
value: null
}, {
id: "bb275e3c-54da-4e85-8cc3-21defff4e278",
key: ["auto", [
2013, 11, 13, 1, 41, 7], "EVERYONE"],
value: null
}]
}
示例数据集。 post_id是文档ID。
{
type: "POST",
post_id: "a4a6cf44-8a82",
visibility: "EVERYONE",
userID: "<user_id>",
title: "Some title 1",
category: "auto",
description: "",
created: "2013-11-07 17:17:46 -0800",
modified: "2013-11-07 17:17:46 -0800"
}, {
type: "POST",
post_id: "a4a6cf44-8a82-494a-a2b9",
visibility: "EVERYONE",
userID: "<user_id>",
title: "Some title 2",
category: "auto",
description: "",
created: "2013-11-07 17:17:46 -0800",
modified: "2013-11-07 17:17:46 -0800"
}, {
type: "POST",
post_id: "a4a6cf44-8a82-494a-a2b9-f6a3ec629f17",
visibility: "EVERYONE",
userID: "<user_id>",
title: "Some title 3",
category: "auto",
description: "",
created: "2013-11-07 17:17:46 -0800",
modified: "2013-11-07 17:17:46 -0800"
}, {
type: "POST",
post_id: "41070cfc-a85c-424c-9b87-fce0616c77c1",
visibility: "EVERYONE",
userID: "<user_id>",
title: "Some title 4",
category: "auto",
description: "",
created: "2013-11-11 12:28:21 -0800",
modified: "2013-11-11 12:28:21 -0800"
}, {
type: "POST",
post_id: "bb275e3c-54da-4e85-8cc3-21defff4e278",
visibility: "EVERYONE",
userID: "<user_id>",
title: "Some title 5",
category: "auto",
description: "",
created: "2013-11-12 17:41:07 -0800",
modified: "2013-11-12 17:41:07 -0800"
}
答案 0 :(得分:0)
好的,这是一个旧帖子,但为了参考起见,我会发布我的发现并尽力解释这个功能。
给定一个包含超过9,000个电影文档的数据库,每个文档包含title
,year
,rating
,genre
等属性,定义视图{{1}这个数据库的索引名称为MovieIndexView
,发出以下复杂(但相当简单)的键,用于按评级过滤电影,并按年份排序:
MovieIndex
可以使用与OP显示方式类似的技术,使用排序行查询视图:
function (doc) {
emit([doc.rating, doc.year], doc);
}
请注意,我们只通过设置- Request -
GET /movies_db/_design/MovieIndex/_view/MovieIndexView?
startkey=["R",{}]
&endkey=["R",0]
&descending=true
&limit=6
- Response -
{
"total_rows": 9411,
"offset": 1,
"rows": [
{
"id": "2802144",
"key": [
"R",
2014
],
"value": {
"_id": "2802144",
"_rev": "1-924e12ba1f1144e3a453bbd8978acc5c",
"title": "Kingsman: The Secret Service",
"year": 2014,
"rating": "R",
"runtime": "129 min",
"genre": [
"Action",
"Adventure",
"Comedy"
],
"director": "Matthew Vaughn"
}
},
<...>
{
"id": "2278388",
"key": [
"R",
2014
],
"value": {
"_id": "2278388",
"_rev": "1-c38b7f5eb43abfd59fb8514277290e46",
"title": "The Grand Budapest Hotel",
"year": 2014,
"rating": "R",
"runtime": "99 min",
"genre": [
"Adventure",
"Comedy",
"Drama"
],
"director": "Wes Anderson"
}
}
]
}
变量(我仅粘贴了第一个和最后一个相关项目)来提取第一个6
文档。
<强>分页强>
下一行序列的第一次分页可能是直截了当的。只需将下一个请求的limit
设置为上一组中最后一项的startkey
值,同时保持key
的值不变:
endkey
你觉得这种方法有问题吗?这个新的开始键无法指定响应应该以{{1}}(具有相同评级的同一电影的电影)具有相同值的多个电影项目中的哪一个。虽然请求没有语法错误,但您会注意到返回的行不会以我们想要的下一个连续项开始,而是使用数据库中另一个具有startkey=["R",2014]
&endkey=["R",0]
&descending=true
&limit=6
匹配值的任意项开始。
这是startkey
派上用场的地方:
key
此请求将返回从序列中的正确项开始的行,从而提供从视图中对行进行分页的最佳方法。
The Takeaway
startkey_docid
必须与相关的- Request -
GET /movies_db/_design/MovieIndexView/_view/MovieIndex?
startkey=["R",2014]
&endkey=["R",0]
&descending=true
&limit=6
&startkey_docid=2278388
一起使用才能使您的分页正常工作。
我希望这可以帮助任何可能在这个微妙特征上破坏头脑的人。
<强>来源强>
CouchDB官方文档:http://docs.couchdb.org/en/latest/couchapp/views/collation.html
有趣的线程交换:http://grokbase.com/t/couchdb/user/091defx51x/sort-by-date-and-find-by-key
答案 1 :(得分:0)
按照以下链接非常清楚地解释如何使用couchDb结果进行分页:
答案 2 :(得分:-1)
您可以在此处找到有关如何正确使用startkey_docid
的更多信息:http://blog.couchbase.com/startkeydocid-behaviour