Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.
The "IsoFormattedDateAsDocId" page has been changed by DougShawhan.
http://wiki.apache.org/couchdb/IsoFormattedDateAsDocId
--------------------------------------------------
New page:
= Using an ISO Formated Date as a Doc _id =
The ISO 8601 http://en.wikipedia.org/wiki/ISO_8601 standard describes a useful scheme for
representing a date string in a Year-Month-DayTHour:Minute:Second.microsecond format. For
time-bound documents in a CouchDB database this can be a very handy way to create a unique
identifier, since javascript can directly use it to create a Date object:
{{{#!javascript
{
"sum_by_day": {
"map": "function(doc) {\n var dt = new Date(doc._id);\n emit(dt.getDate(), 1);\n}",
"reduce": "function(keys, values, rereduce) {\n return sum(values)\n}"
}
}
}}}
Another useful Javascript pattern is to use datetime.substr to create useful values for a
return key:
{{{#!javascript
function (doc) {
var datetime = doc._id;
var year = parseInt(datetime.substr(0, 4));
var month = parseInt(datetime.substr(5, 2), 10);
var day = parseInt(datetime.substr(8, 2), 10);
var hour = parseInt(datetime.substr(11, 2), 10);
var minute = parseInt(datetime.substr(14, 2), 10);
emit([doc.sales, doc.item, year, month, day, hour, minute], 1);
}
}}}
. . . then simply use group_level to zoom in on whatever time you wish to use.
{{{#!javascript
curl -X GET "http://localhost:5984/transaction_history_hps/_design/throwaway/_view/toss?group_level=1"
{"rows":[
{"key":[20],"value":10}
{"key":[21],"value":20}
]}
curl -X GET "http://localhost:5984/transaction_history_hps/_design/throwaway/_view/toss?group_level=2"
{"rows":[
{"key":[20,widget],"value":10}
{"key":[21,widget],"value":10}
{"key":[21,thing],"value":10}
]}
}}}
A nice example using a date within a document is found here: http://www.couchone.com/migrating-to-couchdb#three
|