1

I have a couchdb document.. roughly like this (minimal)

{
  "_id": "225136308d6f95611e457c0f02f1b47a769c35e8",
  "_rev": "19-f2affa088d817ec924c6ca03c34ef1bf",
  "communications": [
    {
      "sequenceId": 1,
      "timestamp": "2019-09-18T05:43:10.412Z"
    },
    {
      "sequenceId": 2,
      "timestamp": "2019-09-18T05:48:37.407Z",
    }
}

I want to look for all documents that has a latest timestamp older than X, how do I create a view to do that seeing as the timestamps are not stored as year,month,day?

1
  • Have you tried any view definition? Can you share it? Commented Oct 10, 2019 at 10:07

1 Answer 1

2

I am not sure about your requirement but you could try something like this

{
  "_id": "_design/sortby",
  "views": {
    "timestamp": {
      "map": "function (doc) {\n  if (doc && doc.communications && Array.isArray(doc.communications)) {\n    doc.communications.forEach(function (communication) {\n      if (communication && communication.timestamp && communication.sequenceId) {\n        var date = new Date(communication.timestamp);\n        emit([date.getFullYear(), date.getMonth(), date.getDate()], communication.sequenceId);\n      }\n    });\n  }\n}"
    }
  },
  "language": "javascript",
  "_rev": "5-12eed6d29b7947eda8caa68be0b17a23"
}

it will create out put view like

{
    "total_rows": 2,
    "offset": 0,
    "rows": [
        {
            "key": [
                2019,
                8,
                18
            ],
            "id": "225136308d6f95611e457c0f02f1b47a769c35e8",
            "value": 1
        },
        {
            "key": [
                2019,
                8,
                18
            ],
            "id": "225136308d6f95611e457c0f02f1b47a769c35e8",
            "value": 2
        }
    ]
}

You can search based on year , month , date

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.