在 CouchDB 中建模博客评论摘要
今天早上我正在探索 CouchDB,并且正在使用一个看起来像这样的文档模式:
{
"post_id": 1,
"date": "1/1/2011",
"body": "ur blog is awesome!"
}
我希望能够构建一个视图,该视图既可以提供给定 post_id 的当前评论数量,也可以提供“ “上次评论”字段告诉我最近发表评论的时间。
我已经玩了足够多的地图/减少来获取评论计数,但没有看到任何方法来包含“最后评论”字段。我想我必须在这里构建两个单独的视图,但想知道是否可以在一次而不是两次中获取此信息。
我目前尝试获取评论数:
map: function(doc) {
emit(doc.post_id, 1);
}
reduce: "_count"
I'm exploring CouchDB this morning, and am playing with a document schema that looks something like this:
{
"post_id": 1,
"date": "1/1/2011",
"body": "ur blog is awesome!"
}
I'd like to be able to build a view that gives me both a current count of comments for a given post_id as well as a 'last commented on' field that tells me when the most recent comment was made.
I have played around enough to do the map / reduce to get the comment count, but do not see any way to include the 'last commented on' field. I'm thinking that I'll have to build two separate views here, but would like to know if it is possible to get this information in one trip instead of two.
My current attempt at getting the comment count:
map: function(doc) {
emit(doc.post_id, 1);
}
reduce: "_count"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,我想我可能拥有它。
如果我将日期切换到纪元,则可以使用 _stats 归约函数拿回我需要的东西。
值得注意的是,_stats 将返回最大值和计数,分别代表我最近的评论日期和总计数。
Actually, I think I may have it.
If I switch my date to an epoch, I can then use the _stats reduce function to get back what I need.
Of note, _stats will return a max value as well as a count, which will represent my most recent comment date and total count respectively.