如何使用CouchDB按年份和月份对条目进行分组? [英] How to group entries by year and month using CouchDB?

查看:106
本文介绍了如何使用CouchDB按年份和月份对条目进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The view function below is used to emit some entries:

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函数按年份和月份对条目进行分组的最佳方法是什么?

What is the best way to group the entries by year and month using CouchDB functions?

(以JSON格式格式化输出,例如:)

(to format output in JSON like:)

{
"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"}] 

    }

}

推荐答案

您的地图函数应如下所示:

Your map function shoud look like this:

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的文档

Then when you query the view data, it will be sorted by date. if you add "descending=true", then you'll get them sorted backwards. And if you add "?startKey=[2012,""]&endkey=[2012]&descending=true", then you'll get all the documents with 2012 doc.date

这篇关于如何使用CouchDB按年份和月份对条目进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆