V
V
vixh2012-05-14 12:53:18
NoSQL
vixh, 2012-05-14 12:53:18

Get the right data in CouchDB?

How to get in CouchDB or how to properly order to get data.
We have rooms (in the hotel) with reservations:

{
   "_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"
}

So we get a list of free rooms from when and to when:
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);
                }
            }
        }
    }

At the exit:
...
{"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)"},
...

How to get free rooms for a period. For example from 2012-05-16 to 2012-05-18?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nekhaychik, 2012-05-14
@gnomeby

But no way. As I already wrote : a base that is not sharpened for processing intervals and time in general. And you have to fence crutches in order to somehow support them.
A crutch: do an emit for every day in between the reservation. And also pass each desired day to the filter for the view. Then you already have only unique entries and sort them as you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question