This is an automated email from the ASF dual-hosted git repository. wohali pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/couchdb-documentation.git
The following commit(s) were added to refs/heads/master by this push: new d116967 Migration of MoinMoin ISO date content d116967 is described below commit d116967e96e5200dd594eb4e8cd8cfbc0f8c1a8e Author: Joan Touzet <jo...@atypical.net> AuthorDate: Mon Dec 17 14:03:52 2018 -0500 Migration of MoinMoin ISO date content --- src/best-practices/index.rst | 1 + src/best-practices/iso-date.rst | 64 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/best-practices/index.rst b/src/best-practices/index.rst index 01b4a05..f7e3ebd 100644 --- a/src/best-practices/index.rst +++ b/src/best-practices/index.rst @@ -26,6 +26,7 @@ system. documents forms + iso-date jsdevel views reverse-proxies diff --git a/src/best-practices/iso-date.rst b/src/best-practices/iso-date.rst new file mode 100644 index 0000000..c91c962 --- /dev/null +++ b/src/best-practices/iso-date.rst @@ -0,0 +1,64 @@ +.. Licensed under the Apache License, Version 2.0 (the "License"); you may not +.. use this file except in compliance with the License. You may obtain a copy of +.. the License at +.. +.. http://www.apache.org/licenses/LICENSE-2.0 +.. +.. Unless required by applicable law or agreed to in writing, software +.. distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +.. WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +.. License for the specific language governing permissions and limitations under +.. the License. + +.. _best-practices/iso-date: + +============================================ +Using an ISO Formatted Date for Document IDs +============================================ + +The `ISO 8601 date standard <http://en.wikipedia.org/wiki/ISO_8601>`_ 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: + +.. code-block:: javascript + + 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. + +.. code-block:: bash + + 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: + +.. code-block:: 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.widget, year, month, day, hour, minute], 1); + }