How to create a compound key with double digit date array?

After reading this blog post,

http://blog.couchbase.com/understanding-grouplevel-view-queries-compound-keys

I am trying to create a compound key with date array containing double digit month and day.
In the blog post, the date array compound key in the resulting row example looks integers.

[2014,03,08]

So I tried the following…

function (doc, meta) {
    var date = doc.date;
    var key = dateToArray(date).slice(0,3);
    emit(key, 1);
}

And I got… [2014,3,8]

Then I tried the following…

function (doc, meta) {
    var date = doc.date;
    var dateArray = dateToArray(date).slice(0,3);
    var year = dateArray[0];
    var month = dateArray[1];
    var day = dateArray[2];

    if (month < 10) {
        month = "0" + month;
    }

    if (day < 10) {
        day = "0" + day;
    }

    var key = [year,month,day];
    emit(key, 1);
}

I got… [2014,“03”,“08”]

Hmmm…

How could I create [2014,03,08] (double digit month and day)?

This is strange question actually. How would you do it in plain javascript or whatever language are you using? I don’t know the language, which can preserve leading zeros in numbers.

It is obviously a typo in that blog post you are referring to, but how it affects you?

UPDATE:

Interesting, javascript handles differently two-digit numbers starting with zero and all others. I thought it behaves like other languages and parse such literals as octal numbers.

~ $ js --version
JavaScript-C 1.8.5 2011-03-31
...
~ $ js
js> 02
2
js> 08
typein:2: warning: 08 is not a legal ECMA-262 octal constant:
typein:2: warning: 08
typein:2: warning: ^
8
js> 012
10
js> 018
typein:1: warning: 08 is not a legal ECMA-262 octal constant:
typein:1: warning: 018
typein:1: warning: ^
18

In the terminal it just shows the warning, but still gives you number like it was without zero. But in chrome it behaves differently

I guess the blog posting is a typo.
I just used single digit(?) month and day, and everything still works fine as I intended.

Thank you.

yes, and also the blog post mentions that internally we are converting them to strings and then collate. this is also not true. it is recursively traverse keys and compare using < operator to order them

1 Like