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?
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