Differences between revisions 9 and 10
Revision 9 as of 2011-02-25 18:37:15
Size: 2491
Editor: DougShawhan
Comment:
Revision 10 as of 2013-04-21 00:25:51
Size: 2603
Comment: added Archive.org cached version of the migrating-to-couchdb doc
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
Line 13: Line 12:
Line 32: Line 30:
Line 46: Line 43:


Line 57: Line 51:
Line 68: Line 61:
A nice example using a date within a document is found here: http://www.couchone.com/migrating-to-couchdb#three A nice example using a date within a document is found here: http://www.couchone.com/migrating-to-couchdb#three [dead link] [[http://web.archive.org/web/20110130042049/http://www.couchone.com/migrating-to-couchdb#three|Archive.org copy]]

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:

{
   "sum_by_day": {
       "map": "function(doc) {\n  var dt = new Date(doc._id);\n  emit([dt.getDate() doc.widget], 1);\n}",
       "reduce": "function(keys, values, rereduce) {\n   return sum(values)\n}"
   }
}

. . . then simply use group_level to zoom in on whatever time you wish to use.

curl -X GET "http://localhost:5984/transactions/_design/widget_count/_view/toss?group_level=1"

{"rows":[
{"key":[20],"value":10},
{"key":[21],"value":20}
]}

curl -X GET "http://localhost:5984/transactions/_design/widget_count/_view/toss?group_level=2"

{"rows":[
{"key":[20,widget],"value":10},
{"key":[21,widget],"value":10},
{"key":[21,thing],"value":10}
]}

Another method is using parseint() and datetime.substr() to cut out useful values for a return key:

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.widget, year, month, day, hour, minute], 1);
}

If you have python views enabled, you can use the datetime module in the same way:

   1 def fun(doc):
   2     from datetime import datetime
   3     dt = datetime.strptime(doc._id, "%Y-%m-%dT%H:%M:%S.%f")
   4     yield [doc.host, dt.year, dt.month, dt.day, dt.hour, dt.minute], 1

. . .example with reduce function as it appears in the design doc:

{
    "doo_dad_finder": {
       "map": "def fun(doc):\n    from datetime import datetime\n    if doc['errors']:\n        dt = datetime.strptime(doc._id, '%Y-%m-%dT%H:%M:%S.%f')\n        for doo_dad in doc['doo_dads']:\n            yield [doo_dad, dt.year, dt.month, dt.hour], 1",
       "reduce": "def fun(keys, values, rereduce):\n    return sum(values)"
   }
}

A nice example using a date within a document is found here: http://www.couchone.com/migrating-to-couchdb#three [dead link] Archive.org copy

IsoFormattedDateAsDocId (last edited 2013-04-21 00:25:51 by BenjaminYoung)