时间戳子字段上的 couchdb query/view
couchdb query/view on timestamp subfields
我有一个 couchdb 文档..大致像这样(最小)
{
"_id": "225136308d6f95611e457c0f02f1b47a769c35e8",
"_rev": "19-f2affa088d817ec924c6ca03c34ef1bf",
"communications": [
{
"sequenceId": 1,
"timestamp": "2019-09-18T05:43:10.412Z"
},
{
"sequenceId": 2,
"timestamp": "2019-09-18T05:48:37.407Z",
}
}
我想查找所有具有早于 X 的最新时间戳的文档,我该如何创建一个视图来执行此操作,因为时间戳未存储为年、月、日?
我不确定你的要求,但你可以尝试这样的事情
{
"_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"
}
它将创建像
这样的输出视图
{
"total_rows": 2,
"offset": 0,
"rows": [
{
"key": [
2019,
8,
18
],
"id": "225136308d6f95611e457c0f02f1b47a769c35e8",
"value": 1
},
{
"key": [
2019,
8,
18
],
"id": "225136308d6f95611e457c0f02f1b47a769c35e8",
"value": 2
}
]
}
您可以根据年、月、日进行搜索
我有一个 couchdb 文档..大致像这样(最小)
{
"_id": "225136308d6f95611e457c0f02f1b47a769c35e8",
"_rev": "19-f2affa088d817ec924c6ca03c34ef1bf",
"communications": [
{
"sequenceId": 1,
"timestamp": "2019-09-18T05:43:10.412Z"
},
{
"sequenceId": 2,
"timestamp": "2019-09-18T05:48:37.407Z",
}
}
我想查找所有具有早于 X 的最新时间戳的文档,我该如何创建一个视图来执行此操作,因为时间戳未存储为年、月、日?
我不确定你的要求,但你可以尝试这样的事情
{
"_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"
}
它将创建像
这样的输出视图{
"total_rows": 2,
"offset": 0,
"rows": [
{
"key": [
2019,
8,
18
],
"id": "225136308d6f95611e457c0f02f1b47a769c35e8",
"value": 1
},
{
"key": [
2019,
8,
18
],
"id": "225136308d6f95611e457c0f02f1b47a769c35e8",
"value": 2
}
]
}
您可以根据年、月、日进行搜索