1

I have documents in my bucket:

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

I have got view:

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

it gives me:

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

I want to select posts of specific users ["2","3","5"] ordered by timestamp. I mean "get the newest posts of some users". Is it possible to do?

Łukasz
  • 623
  • 5
  • 12

2 Answers2

0

I would re-organize your data: Assuming your authors are unique, keep a document for each author and save their posts as a list within that doc.

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

The trick is to maintain order within the list of posts. Your view can pull the last item in the list of posts if you want only the latest post.

FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79
0

View key array deals with its (natural) order. If you want to "specific" user: say, exact matched user with timestamp ordered, the view function would look like:

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

with this exact author 1, timestamp range 1~100
startkey: [1, 1]
endkey: [1, 100]

results:
[1, 11]
[1, 12]

BUT, if you want both attribute with range...

author: 1~10
timestamp: 1~100

It follows emitted 'ARRAY' order; more than 2 attribute with range query will not return as desired way.
If you want another type of sorted result with multiple "ranged" attribute, you should consist view function.

Intae Kim
  • 309
  • 1
  • 5