CouchDB 治疗床底座startkey_docid和复杂密钥

x8diyxa7  于 2022-12-09  发布在  CouchDB
关注(0)|答案(2)|浏览(129)

以下具有匹配结果集的调用返回的结果不是以文档ID a4 a6 cf 44 - 8a 82 - 494 a-a2 b 9-f6 a3 ec 629 f17开头。如以下结果集所示,前3个键相同,但startkey_docid无效。
就问几个问题

  • startkey_docid能正确处理复杂的键吗?(这显然适用于couchdb)
  • 如果上述问题答案为“是”,那么在我们看来,问题的来源是否是使用dateToArray(创建的文档)?
  • 这是一个错误吗?

查看方式:

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是文档标识。

{
    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"
}
ogsagwnx

ogsagwnx1#

好吧,这是一个老线索,但为了参考起见,我将张贴我的发现,并试图解释这个功能,我可以。

示例

给定一个包含9,000多个电影文档的数据库,每个文档都包含titleyearratinggenre等属性,为该数据库定义一个索引名为MovieIndex的视图MovieIndexView,该视图发出以下复杂(但相当简单)的键,用于按分级筛选电影,并按年份排序:

function (doc) {
    emit([doc.rating, doc.year], doc);
}

可以使用类似于OP中所示的方法来查询具有已排序行的视图:

- 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"
            }
        }
    ]
}

请注意,我们只通过设置limit变量提取了第一个6文档(我只粘贴了第一个和最后一个相关项)。

分页

对下一个行序列进行分页的第一步可以是直接的,只需将下一个请求的startkey设置为上一个集合中最后一个项的key值,同时保持endkey的值不变:

startkey=["R",2014]
&endkey=["R",0]
&descending=true
&limit=6

这个新的开始键无法指定多个电影项中的哪一个具有相同的startkey值(具有相同评级的电影,在同一年上映)响应应该以开头。虽然请求在语法上没有错误,但您会注意到返回的行不会以我们想要的下一个顺序项开头,而是与数据库中具有key的匹配值的另一个任意项相关联。
这就是startkey_docid派上用场的地方:

- Request -
GET /movies_db/_design/MovieIndexView/_view/MovieIndex?
startkey=["R",2014]
&endkey=["R",0]
&descending=true
&limit=6
&startkey_docid=2278388

此请求将返回以序列中的正确项开头的行,从而提供了对视图中的行进行分页的最佳方法。
"外卖"
startkey_docid必须与相关的startkey一起使用,才能进行分页。
我希望这有助于任何人谁可能是打破他们的头上这个微妙的特点。

来源

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
类似SO主题:https://stackoverflow.com/questions/10142850/need-a-couchdb-trick-to-sort-by-date-and-filter-by-group#=

z31licg0

z31licg02#

您可以在此处找到有关如何正确使用startkey_docid的更多信息:http://blog.couchbase.com/startkeydocid-behaviour

相关问题