Сортировка сложных ключей в диване БД - PullRequest
0 голосов
/ 24 мая 2018

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

1 Ответ

0 голосов
/ 24 мая 2018

Этот вопрос помечен как "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.

Подробное описание порядка упорядочения представлений подробно объясняется здесь .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...