如何使用 CouchDB 按年和月对条目进行分组?

Posted

技术标签:

【中文标题】如何使用 CouchDB 按年和月对条目进行分组?【英文标题】:How to group entries by year and month using CouchDB? 【发布时间】:2012-12-01 17:59:57 【问题描述】:

下面的视图函数用于发出一些条目:

function(doc) emit(null,"date":doc.date,"title":doc.title,"txt":doc.txt);

"total_rows":7,"offset":0,"rows":[
"id":"67ebe3755be4edf5c4edf0d96f0023eb","key":null,"value":"date":"Dec 12,2012","title":"test1","txt":"this is just a test",
"id":"67ebe3755be4edf5c4edf0d96f003120","key":null,"value":"date":"Nov 11,2012","title":"test2","txt":"this is just a test2",
"id":"67ebe3755be4edf5c4edf0d96f003869","key":null,"value":"date":"Dec 22,2012","title":"test4","txt":"this is just a test4",
"id":"67ebe3755be4edf5c4edf0d96f003cfd","key":null,"value":"date":"Aug 21,2010","title":"test5","txt":"this is just a test5",
"id":"67ebe3755be4edf5c4edf0d96f004466","key":null,"value":"date":"Nov 1, 2010","title":"test6","txt":"this is just a test6",
"id":"67ebe3755be4edf5c4edf0d96f004d9c","key":null,"value":"date":"Aug 15,2010","title":"test7","txt":"this is just a test7",
"id":"67ebe3755be4edf5c4edf0d96f005d04","key":null,"value":"date":"Feb 28,2012","title":"test3","txt":"this is just a test3"
]

使用 CouchDB 函数按年和月对条目进行分组的最佳方法是什么?

(以 JSON 格式输出格式:)


"2012":
    "Feb":["date":"Feb 28,2012","title":"test3","txt":"this is just a test3"],
    "Nov":["date":"Nov 11,2012","title":"test2","txt":"this is just a test2"],
    "Dec":["date":"Dec 12,2012","title":"test1","txt":"this is just a test",
        "date":"Dec 22,2012","title":"test4","txt":"this is just a test4"]    

    ,

"2010":
    "Aug":["date":"Aug 15,2010","title":"test7","txt":"this is just a test7",
           "date":"Aug 21,2010","title":"test5","txt":"this is just a test5"],
    "Nov":["date":"Nov 1, 2010","title":"test6","txt":"this is just a test6"] 

    


【问题讨论】:

【参考方案1】:

您的地图功能应如下所示:

function (doc) 
    var date = new Date(doc.date);
    emit([date.getFullYear(), date.getMonth()], doc);

那么当你查询视图数据时,它会按日期排序。如果你添加“descending=true”,那么你会让它们倒序排列。如果你加上"?startKey=[2012,""]&endkey=[2012]&descending=true",那么你会得到所有带有2012 doc.date的文件

【讨论】:

感谢您的帮助 - 它完美无瑕 - 我只将查询参数更改为小写:startKey --> startkey. 别忘了接受下一个搜索相同问题的人的正确答案;)

以上是关于如何使用 CouchDB 按年和月对条目进行分组?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Flume 按年和月对 txt/csv 文件中的数据进行分区?是不是可以使 HDFS 路径动态化?

我需要从结构为 yyyyMMddHHmmss 20170227141500 的时间戳中按年和月分组

按年和月分组并获得一个月的最小值,日期

c# List<T> 按年分组,然后按月分组

如何简化 Laravel 查询以按年搜索日期并按月分组

请求有关如何在一个查询中对多个条件的计数进行分组的帮助