在 couchdb 中对复杂键进行排序
Sorting complex keys in couch db
我正在尝试学习沙发数据库排序机制。
我读过这个 link 但是当我按照页面建议测试一个小问题时我很困惑。
页面显示
First thing of note and *very* important, even though this is an array output that seems like integers from the javascript Map function, they are not, each of those Index Keys are strings, and are ordered character by character as strings, including the brackets and commas, notice that all single digits are padded with zeros in front, and that is why the order is maintained. It's more like this, so we'll go ahead and keep the quote characters:
If you had the following Map output, notice that it is sorted differently than it would be if the Int parameters were actually Int's, in fact Index Keys are always strings.
[2012,”beer”,1]
null
[2012,”beer”,10]
null
[2012,”beer”,2]
null
Notice that the second “element” of the Index Key is ordered to be before the 3rd because of string compare, these are not integers. Back to the scheduled program…
据我所知,复杂键被认为是一个字符串,即使其中包含整数值(包括引号和括号)所以我尝试了以下操作。
function (doc) {
emit([doc.changeDate,doc.terminalUser.userName], doc._id);
}
结果是。
{
"total_rows": 466,
"offset": 0,
"rows": [
{
"id": "b1bf0dad-bf55-4e5f-86d7-830dd1aa3415",
"key": [
2,
"test"
],
"value": "b1bf0dad-bf55-4e5f-86d7-830dd1aa3415"
},
{
"id": "ccab524a-ae6c-4131-a1af-bfccb5e70cff",
"key": [
3,
"test"
],
"value": "ccab524a-ae6c-4131-a1af-bfccb5e70cff"
},
{
"id": "fa08d5e0-e430-4c9d-8340-d5db459ad67d",
"key": [
1524823966903,
"test"
],
"value": "fa08d5e0-e430-4c9d-8340-d5db459ad67d"
},
{
"id": "aab5f103-6a65-4a19-9c1c-2749abec361b",
"key": [
1524824434308,
"test"
],
"value": "aab5f103-6a65-4a19-9c1c-2749abec361b"
},
{
"id": "189a6d1c-d80d-4006-9852-4e17649b8d0e",
"key": [
1524824436016,
"test"
],
"value": "189a6d1c-d80d-4006-9852-4e17649b8d0e"
}
]
}
我的问题是,如果复合键完全被认为是一个字符串,那么带有键 [2,"test"] 的条目为什么排在带有键 [1524823966903,"test"] 的条目之前?似乎整数被排序为整数而不是字符串。有什么想法吗?
这个问题被标记了 "couchdb" 但你指的是 Couchbase,这是另一回事 ;)
在 CouchDB 中,排序与您在问题中提供的不同:
{"total_rows":3,"offset":0,"rows":[
{"id":"8b71d31c90836995f27d8c379442862c","key":[2012,"beer",1],"value":null},
{"id":"2fcf6ee7c3e4b4175c7ac2a95b70d7a3","key":[2012,"beer",2],"value":null},
{"id":"8b71d31c90836995f27d8c379444d5bd","key":[2012,"beer",10],"value":null}
]}
以上示例显示数组的第三个元素被视为数字,给出 1,2,10 排序顺序。
详细解释了视图的排序方式here。
我正在尝试学习沙发数据库排序机制。 我读过这个 link 但是当我按照页面建议测试一个小问题时我很困惑。
页面显示
First thing of note and *very* important, even though this is an array output that seems like integers from the javascript Map function, they are not, each of those Index Keys are strings, and are ordered character by character as strings, including the brackets and commas, notice that all single digits are padded with zeros in front, and that is why the order is maintained. It's more like this, so we'll go ahead and keep the quote characters:
If you had the following Map output, notice that it is sorted differently than it would be if the Int parameters were actually Int's, in fact Index Keys are always strings.
[2012,”beer”,1]
null
[2012,”beer”,10]
null
[2012,”beer”,2]
null
Notice that the second “element” of the Index Key is ordered to be before the 3rd because of string compare, these are not integers. Back to the scheduled program…
据我所知,复杂键被认为是一个字符串,即使其中包含整数值(包括引号和括号)所以我尝试了以下操作。
function (doc) {
emit([doc.changeDate,doc.terminalUser.userName], doc._id);
}
结果是。
{
"total_rows": 466,
"offset": 0,
"rows": [
{
"id": "b1bf0dad-bf55-4e5f-86d7-830dd1aa3415",
"key": [
2,
"test"
],
"value": "b1bf0dad-bf55-4e5f-86d7-830dd1aa3415"
},
{
"id": "ccab524a-ae6c-4131-a1af-bfccb5e70cff",
"key": [
3,
"test"
],
"value": "ccab524a-ae6c-4131-a1af-bfccb5e70cff"
},
{
"id": "fa08d5e0-e430-4c9d-8340-d5db459ad67d",
"key": [
1524823966903,
"test"
],
"value": "fa08d5e0-e430-4c9d-8340-d5db459ad67d"
},
{
"id": "aab5f103-6a65-4a19-9c1c-2749abec361b",
"key": [
1524824434308,
"test"
],
"value": "aab5f103-6a65-4a19-9c1c-2749abec361b"
},
{
"id": "189a6d1c-d80d-4006-9852-4e17649b8d0e",
"key": [
1524824436016,
"test"
],
"value": "189a6d1c-d80d-4006-9852-4e17649b8d0e"
}
]
}
我的问题是,如果复合键完全被认为是一个字符串,那么带有键 [2,"test"] 的条目为什么排在带有键 [1524823966903,"test"] 的条目之前?似乎整数被排序为整数而不是字符串。有什么想法吗?
这个问题被标记了 "couchdb" 但你指的是 Couchbase,这是另一回事 ;)
在 CouchDB 中,排序与您在问题中提供的不同:
{"total_rows":3,"offset":0,"rows":[
{"id":"8b71d31c90836995f27d8c379442862c","key":[2012,"beer",1],"value":null},
{"id":"2fcf6ee7c3e4b4175c7ac2a95b70d7a3","key":[2012,"beer",2],"value":null},
{"id":"8b71d31c90836995f27d8c379444d5bd","key":[2012,"beer",10],"value":null}
]}
以上示例显示数组的第三个元素被视为数字,给出 1,2,10 排序顺序。
详细解释了视图的排序方式here。