4.3. Using an ISO Formatted Date for Document IDs — Apache CouchDB® 3.2 Documentation

# 4.3. Using an ISO Formatted Date for Document IDs

The ISO 8601 date 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. Using this sample `map` function:

    function(doc) {
      var dt = new Date(doc._id);
      emit([dt.getDate(), doc.widget], 1);
    }

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);
    }