Я пытаюсь изучить механизм сортировки даба couch.Я прочитал эту ссылку , но когда я тестирую небольшую проблему на странице, я запутался.
На странице написано
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, «тест»] упорядочена перед записью с ключом [1524823966903, «тест»] если составной ключ вообще считается строкой?Кажется, целое число сортируется как целое число, а не строка.Есть идеи?