sachin
sachin

Reputation: 14365

How to query the CouchDB data with the start date and end date

In my node application I am using the Cradle library to save the data in CouchDB. Now I want to get the stored data with the specified start date and end date. How can I do this. I have added design document as below. Is it correct? How to pass my parameters to this function?

function (doc) {
    if (doc.date) {
        var key = [doc.start, doc.end];
        emit(key, doc);
    }
}

Document Structure

       {
         "_id": "58113f948561d38b1eba8ba343432c45",
         "_rev": "1-be4f3d2fd93a8941659c73333941561d",
         "date": "2013-04-29T06:36:29.165Z",
         "name":"sdasd",
         "age":"20"
       }

I'm querying like below for the above design document

var startDate = '2013-04-27T08:41:23.559Z';
var endDate = '2013-04-29T08:41:23.559Z';

db.view('data/byDate', { key: [startDate, endDate] }, function(err, doc) {
    if (!err) {
        console.log(doc.length);
    } else {
        console.log('Error '+err);
    }
});

Upvotes: 2

Views: 544

Answers (1)

brdlph
brdlph

Reputation: 619

The structure of your example document and your view do not match: You try to emit doc.start and doc.end but these fields are not present (hence the document will not be included in the view). Maybe you just want to emit doc.date? Like so:

function (doc) {
    if (doc.date) {
        emit(doc.date, doc);
    }
}

Upvotes: 0

Related Questions