couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Bonser <mister...@gmail.com>
Subject Re: Next and previous element.
Date Tue, 22 Jun 2010 12:44:17 GMT
On Tue, Jun 22, 2010 at 12:34 AM, Randall Leeds <randall.leeds@gmail.com> wrote:
> Not so easy, Paul. Descending only reverses the output, it doesn't
> actually start backward from startkey.

What? Yes it does! That's exactly what it does! That's why when you
add descending=true, you have to swap your startkey and endkey. But in
this case, he want the item immediately before and after the current
item, so he can use the same key.

I checked to make sure you didn't know something I didn't:

    $ curl localhost:5984/orderdb/_all_docs
    {"total_rows":3,"offset":0,"rows":[
    {"id":"a1","key":"a1","value":{"rev":"1-6e15c8b45b11569dcce6e9ebe57e7ed0"}},
    {"id":"a2","key":"a2","value":{"rev":"1-8a6a10dcc1da8f963978d778f63186ab"}},
    {"id":"a3","key":"a3","value":{"rev":"1-8b1e13f02821b82f945f1459202db018"}}
    ]}

    $ curl 'localhost:5984/orderdb/_all_docs?startkey=%22a2%22&skip=1'
    {"total_rows":3,"offset":1,"rows":[
    {"id":"a3","key":"a3","value":{"rev":"1-8b1e13f02821b82f945f1459202db018"}}
    ]}

    $ curl 'localhost:5984/orderdb/_all_docs?startkey=%22a2%22&skip=1&descending=true'
    {"total_rows":3,"offset":2,"rows":[
    {"id":"a1","key":"a1","value":{"rev":"1-6e15c8b45b11569dcce6e9ebe57e7ed0"}}
    ]}



>
> On Mon, Jun 21, 2010 at 18:21, Paul Bonser <misterpib@gmail.com> wrote:
>> What you probably want to use for your two queries is:
>>
>> http://localhost:5984/troppotardi/_design/images/_view/slug_by_time?startkey_docid={current_docid}&limit=1&skip=1
>> and
>> http://localhost:5984/troppotardi/_design/images/_view/slug_by_time?startkey_docid={current_docid}&limit=1&skip=1&descending=true
>>
>> Also, you could use a timestamp and a client-generated sequence number
>> to distinguish between docs with the same timestamp, then in your view
>>
>>    emit([doc.timestamp, doc.sequence], {slug: doc.slug});
>>
>> Then you could just use the timestamp and sequence of the current doc
>> as the startkey of the view query. Or you could use the slug as the
>> second value of the key, assuming you know you won't have duplicate
>> slugs for a given timestamp (looks like that must be, since you're
>> identifying things by their slug).
>>
>> On Fri, Jun 18, 2010 at 6:37 PM, Francesco Mazzoli
>> <francesco.mazzoli09@imperial.ac.uk> wrote:
>>> Right now I have solved the situation using sequential ids (before it
>>> didn't work because my view was wrong), but it's quite an ugly
>>> solution.
>>> I was trying to use startkey_docid and endkey_docid but I think I am
>>> missing something: http://pastebin.com/XKyvNtGU
>>> -- Francesco
>>>
>>>
>>>
>>> On 18 June 2010 17:54, Francesco Mazzoli <fm2209@ic.ac.uk> wrote:
>>>> This is exactly what I was trying to do, but with sequential ids (I
>>>> can't really use a timestamp since articles get accepted in groups, so
>>>> they end up having the same timestamp)
>>>> The problem is that I don't get the desired result.
>>>> For example now I am using this view (slug_by_time):
>>>> function(doc) {
>>>>    if (doc.type == 'Image' && doc.accepted) {
>>>>        emit(doc.id, {slug: doc.slug});
>>>>    }
>>>> }
>>>> And, having one id, I am making request like:
>>>> /troppotardi/_design/images/_view/slug_by_time?endkey=%2239f56554dba0e810d5a8d17225000f0f%22
>>>> to get the previous slugs and
>>>> /troppotardi/_design/images/_view/slug_by_time?endkey=%2239f56554dba0e810d5a8d17225000f0f%22
>>>> to get the next one, but it doesn't work...
>>>>
>>>> -- Francesco
>>>>
>>>>
>>>>
>>>> On 18 June 2010 15:27, J Chris Anderson <jchris@gmail.com> wrote:
>>>>>
>>>>> On Jun 18, 2010, at 5:31 AM, Francesco Mazzoli wrote:
>>>>>
>>>>>> This may be a simple question but I couldn't find a solution by myself.
>>>>>> I am coding a web application with pylons (pylonshq.com) and couchdb,
>>>>>> and I've stumbled upon a problem.
>>>>>> It's a blog, and I assign a slug to every article. I get the slug
from
>>>>>> a get parameter in the address.
>>>>>> I would like, from that article, to get the next and the previous
one.
>>>>>> I am using sequential uuids, and I've got this view slug_by_time
that
>>>>>> returns the docs slugs with the ids as keys. In this way they are
>>>>>> ordered by time of creation.
>>>>>> The problem is that, having one id, I can't get the previous and
next
>>>>>> element. My idea was simply to use the current id as startkey to
find
>>>>>> the next one end endkey to find the previous one, but I get weird
>>>>>> results.
>>>>>
>>>>> if you store a timestamp on the item, you can have a view by timestamp
and query with limit 1 on each side of the known timestamp for next and previous.
>>>>>
>>>>> Chris
>>>>>
>>>>>>
>>>>>> Any suggestions?
>>>>>> Thanks in advance,
>>>>>> -- Francesco
>>>>>
>>>>>
>>>>
>>>
>>
>>
>>
>> --
>> Paul Bonser
>> http://probablyprogramming.com
>>
>



-- 
Paul Bonser
http://probablyprogramming.com

Mime
View raw message