2015-07-13 45 views
1

我有我的桶的文件:quering couchbase意见导致

{ type: "post", author: "1", subject: "s1", timestamp: 11} 
{ type: "post", author: "1", subject: "s2", timestamp: 12} 
{ type: "post", author: "2", subject: "s3", timestamp: 9 } 
{ type: "post", author: "3", subject: "s4", timestamp: 10} 
{ type: "post", author: "4", subject: "s5", timestamp: 14} 
{ type: "post", author: "5", subject: "s6", timestamp: 11} 

我有看法:

function(doc, meta) { 
    if(doc.t === 'post') { 
    emit([doc.timestamp, doc.author]); 
    } 
} 

它给了我:

[11, "1"] 
[12, "1"] 
[9, "2"] 
[10, "3"] 
[14, "4"] 
[11, "5"] 

我想要选择按时间戳排序的特定用户的帖子[“2”,“3”,“5”]。 我的意思是“获取某些用户的最新帖子”。 可以做吗?

回答

0

我将重新整理您的数据: 假设您的作者是唯一的,请为每位作者保留一份文档,并将其文章保存为该文档中的列表。

doc name: "author:1" 
doc: 
{ 'posts':[{'subject':'s1','body':'this is body1'}, 
      {'subject':'s2','body':'this is body2'}] } 

诀窍是维护职位列表内的顺序。如果您只希望发布最新帖子,您的视图可以提取帖子列表中的最后一项。

0

查看关键数组处理其自然顺序。 如果你想“特定”用户:说,与时间戳精确匹配的用户订购,视图功能将类似于:

function(doc, meta) { 
    if(doc.type === 'post') { 
     emit([doc.author, doc.timestamp], null); 
    } 
} 

与此完全相同笔者1,时间戳范围1〜100
startkey: [1,1]
endkey:[1,100]

结果:
[1,11]
[1,12]

但是,如果你想与范围都属性...

作者:1〜10
时间戳:1〜100

它遵循 '发射阵' 秩序;带范围查询的超过2个属性将不会按照所需的方式返回。
如果您想要使用多个“ranged”属性的另一种排序结果,则应该包含view函数。