Как в CouchDB получить или как правильно упорядочить что бы получить данные.
Имеем номера(в отеле) с резервациями:
{
"_id": "1(2+1)",
"_rev": "1-1a4fbdf79a2da6195ba07cbf0936007f",
"reservations": [
{
"check_in": "2012-01-01",
"check_out": "2012-01-02",
"name": "BENA"
},
{
"check_in": "2012-01-05",
"check_out": "2012-01-08",
"name": "BENA"
},
...
{
"check_in": "2012-08-30",
"check_out": "2012-09-02",
"name": "Radek"
},
{
"check_in": "2012-09-20",
"check_out": "2012-09-23",
"name": "Kárpáteurópa"
}
],
"type": "room"
}
Так получим список свободных номеров от когда и до когда:
availability: {
map: function (doc) {
if(doc.type == 'room'){
var len = doc.reservations.length - 1;
for(var i=0; i<len; i++){
var next = i + 1;
if (doc.reservations[i].check_out == doc.reservations[next].check_in){
continue;
}
var avalFrom = doc.reservations[i].check_out;
var avalTill = doc.reservations[next].check_in;
var avalNights = (Date.parse(avalTill.replace(/-/g, '/')) - Date.parse(avalFrom.replace(/-/g, '/'))) / 86400000;
emit([avalNights, doc.reservations[i].check_out, doc.reservations[next].check_in, doc._id], doc._id);
}
}
}
}
На выходе:
...
{"id":"19(2)","key":[3,"2012-05-13","2012-05-16","19(2)"],"value":"19(2)"},
{"id":"21(2)","key":[3,"2012-05-13","2012-05-16","21(2)"],"value":"21(2)"},
{"id":"22(2)","key":[3,"2012-05-13","2012-05-16","22(2)"],"value":"22(2)"},
{"id":"5(2)","key":[3,"2012-05-13","2012-05-16","5(2)"],"value":"5(2)"},
{"id":"6(2)","key":[3,"2012-05-13","2012-05-16","6(2)"],"value":"6(2)"},
{"id":"8(2+1)","key":[3,"2012-05-13","2012-05-16","8(2+1)"],"value":"8(2+1)"},
{"id":"1(2+1)","key":[3,"2012-05-21","2012-05-24","1(2+1)"],"value":"1(2+1)"},
{"id":"14(2)","key":[3,"2012-05-21","2012-05-24","14(2)"],"value":"14(2)"},
{"id":"2(2)","key":[3,"2012-05-21","2012-05-24","2(2)"],"value":"2(2)"},
{"id":"18(2)","key":[3,"2012-05-22","2012-05-25","18(2)"],"value":"18(2)"},
{"id":"19(2)","key":[3,"2012-05-22","2012-05-25","19(2)"],"value":"19(2)"},
{"id":"20(2)","key":[3,"2012-05-22","2012-05-25","20(2)"],"value":"20(2)"},
{"id":"21(2)","key":[3,"2012-05-22","2012-05-25","21(2)"],"value":"21(2)"},
{"id":"22(2)","key":[3,"2012-05-22","2012-05-25","22(2)"],"value":"22(2)"},
...
Как получить свободные номера на период. Например от 2012-05-16 до 2012-05-18?