在沙发数据库中对复杂键进行排序

时间:2018-05-24 13:29:46

标签: nosql couchdb

我正在尝试学习沙发数据库分类机制。 我已经阅读了这个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"
        }
    ]
}

我的问题是,如果复合键被认为是复合键,为什么在使用键[1524823966903," test"]进入之前,如何输入带有键[2," test"]的条目?字符串一共?似乎整数按整数排序而不是字符串。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这个问题被标记为“couchdb”,但你引用的是Couchbase,这是另一回事;)

在CouchDB中,排序不是您提出的问题:

Y(z)

上面的例子显示数组的第三个元素被视为一个数字,给出了1,2,10的排序顺序。

详细解释了视图的排序方式here

相关问题