You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Ross Bates <rb...@gmail.com> on 2009/06/18 00:46:48 UTC

startkey/endkey and descending=true

Hi All - I have a view that uses an array for it's key. I want to select by
the first value and sort by the second in descending order (it sorts
properly in ascending order by default)

I'm using this syntax to select the rows and everything works fine

http://couch:5984/msg_db/_view/views/msg_by_tag_date?startkey=[%22Tag%22]&endkey=[%22Tag%22,{}]

but when I add the descending parameter like this:

http://couch:5984/msg_db/_view/views/msg_by_tag_date?descending=true&startkey=[%22Tag%22]&endkey=[%22Tag%22,{}]

couch.log indicates a 200 but I get 0 rows back.

Is what am I trying to do possible?

Thanks for any help!

-Ross

Re: startkey/endkey and descending=true

Posted by Ross Bates <rb...@gmail.com>.
Eureka! Thanks Paul and Blair - your examples and explanations make sense
and were very helpful.

-Ross



On Wed, Jun 17, 2009 at 8:11 PM, Paul Davis <pa...@gmail.com>wrote:

> Blair has the right idea here.
>
> descending=true quite literally means "traverse the btree backwards"
> and that is all. The side effect this has that catches a lot of people
> is that "traverse the btree backwards" means you have to swap your
> start and end keys because going backwards the logic is swapped. Or in
> other words, "start" means "encountered first, given the current
> traversal direction".
>
> HTH,
> Paul Davis
>
> On Wed, Jun 17, 2009 at 8:04 PM, Blair Nilsson<bl...@gmail.com>
> wrote:
> > I shouldn't drink so much coffee before posting, some of that didn't
> > make too much sense.
> >
> > take the keys
> > ["a",1]
> > ["a",2]
> > ["a",3]
> > ["b",1]
> > ["b",3]
> > ["b",5]
> >
> > startkey=["a"]&endkey=["b"]
> > will give you all of the keys starting with ["a"]
> >
> > Any key after ["a",3] and before ["b",1] can be used.
> > {} is after any number, string or array (I think), so is very useful
> > to use as part of a key describing a range.
> >
> > startkey=["a"]&endkey=["a",{}] should give you anything under ["a"],
> > since it is the range containing going form the first to last possible
> > key for anything under ["a"]
> >
> > What that means when you use descending=true, you will need to start
> > with ["a",{}] and end with ["a"]
> >
> > Hopefully, this will make more sense, and that I have will remember to
> > read my posts before I post them :)
> >
> > Sorry for the hurried post before.
> >
> > --- Blair
> >
>

Re: startkey/endkey and descending=true

Posted by Paul Davis <pa...@gmail.com>.
Blair has the right idea here.

descending=true quite literally means "traverse the btree backwards"
and that is all. The side effect this has that catches a lot of people
is that "traverse the btree backwards" means you have to swap your
start and end keys because going backwards the logic is swapped. Or in
other words, "start" means "encountered first, given the current
traversal direction".

HTH,
Paul Davis

On Wed, Jun 17, 2009 at 8:04 PM, Blair Nilsson<bl...@gmail.com> wrote:
> I shouldn't drink so much coffee before posting, some of that didn't
> make too much sense.
>
> take the keys
> ["a",1]
> ["a",2]
> ["a",3]
> ["b",1]
> ["b",3]
> ["b",5]
>
> startkey=["a"]&endkey=["b"]
> will give you all of the keys starting with ["a"]
>
> Any key after ["a",3] and before ["b",1] can be used.
> {} is after any number, string or array (I think), so is very useful
> to use as part of a key describing a range.
>
> startkey=["a"]&endkey=["a",{}] should give you anything under ["a"],
> since it is the range containing going form the first to last possible
> key for anything under ["a"]
>
> What that means when you use descending=true, you will need to start
> with ["a",{}] and end with ["a"]
>
> Hopefully, this will make more sense, and that I have will remember to
> read my posts before I post them :)
>
> Sorry for the hurried post before.
>
> --- Blair
>

Re: startkey/endkey and descending=true

Posted by Blair Nilsson <bl...@gmail.com>.
I shouldn't drink so much coffee before posting, some of that didn't
make too much sense.

take the keys
["a",1]
["a",2]
["a",3]
["b",1]
["b",3]
["b",5]

startkey=["a"]&endkey=["b"]
will give you all of the keys starting with ["a"]

Any key after ["a",3] and before ["b",1] can be used.
{} is after any number, string or array (I think), so is very useful
to use as part of a key describing a range.

startkey=["a"]&endkey=["a",{}] should give you anything under ["a"],
since it is the range containing going form the first to last possible
key for anything under ["a"]

What that means when you use descending=true, you will need to start
with ["a",{}] and end with ["a"]

Hopefully, this will make more sense, and that I have will remember to
read my posts before I post them :)

Sorry for the hurried post before.

--- Blair

Re: startkey/endkey and descending=true

Posted by Paul Davis <pa...@gmail.com>.
Good call.

On Thu, Jun 18, 2009 at 4:52 PM, Chris Anderson<jc...@apache.org> wrote:
> On Wed, Jun 17, 2009 at 4:39 PM, Blair Nilsson<bl...@gmail.com> wrote:
>
>> but if we use
>> group=true&descending=true&startkey=["Mark",4]&endkey=["Mark",4,{}]
>>
>> we get
>> {"rows":[]}
>>
>
> It would be fairly easy to throw an error when the startkey is after
> the endkey (eg it's impossible that there could be query result rows)
> and it would make this a lot easier for users to debug. I'll add a
> jira ticket.
>
> Chris
>
> --
> Chris Anderson
> http://jchrisa.net
> http://couch.io
>

Re: startkey/endkey and descending=true

Posted by Chris Anderson <jc...@apache.org>.
On Wed, Jun 17, 2009 at 4:39 PM, Blair Nilsson<bl...@gmail.com> wrote:

> but if we use
> group=true&descending=true&startkey=["Mark",4]&endkey=["Mark",4,{}]
>
> we get
> {"rows":[]}
>

It would be fairly easy to throw an error when the startkey is after
the endkey (eg it's impossible that there could be query result rows)
and it would make this a lot easier for users to debug. I'll add a
jira ticket.

Chris

-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Re: startkey/endkey and descending=true

Posted by Blair Nilsson <bl...@gmail.com>.
It is a range,[%22foo%22] is at the top, and [%22foo%22,{}] is right
down the bottom. since {} comes in the sort, I think after everything
else that can be there, certainly after strings and numbers.

For a query we use fairly often,

group=true&startkey=["Mark",4]&endkey=["Mark",4,{}]
gives us
{"rows":[
{"key":["Mark",4,"2009/06/10 00:00:00"],"value":[20,20,45]},
{"key":["Mark",4,"2009/06/11 00:00:00"],"value":[32,29,51]},
{"key":["Mark",4,"2009/06/12 00:00:00"],"value":[34,33,50]},
{"key":["Mark",4,"2009/06/13 00:00:00"],"value":[33,33,45]},
{"key":["Mark",4,"2009/06/14 00:00:00"],"value":[42,37,48]},
{"key":["Mark",4,"2009/06/15 00:00:00"],"value":[56,51,49]},
{"key":["Mark",4,"2009/06/16 00:00:00"],"value":[17,24,49]},
{"key":["Mark",4,"2009/06/17 00:00:00"],"value":[48,43,49]},
{"key":["Mark",4,"2009/06/18 00:00:00"],"value":[37,26,49]}
]}

to produce a reverse, we have to use
group=true&descending=true&startkey=["Mark",4,{}]&endkey=["Mark",4]

which will give us...

{"rows":[
{"key":["Mark",4,"2009/06/18 00:00:00"],"value":[37,26,49]},
{"key":["Mark",4,"2009/06/17 00:00:00"],"value":[48,43,49]},
{"key":["Mark",4,"2009/06/16 00:00:00"],"value":[17,24,49]},
{"key":["Mark",4,"2009/06/15 00:00:00"],"value":[56,51,49]},
{"key":["Mark",4,"2009/06/14 00:00:00"],"value":[42,37,48]},
{"key":["Mark",4,"2009/06/13 00:00:00"],"value":[33,33,45]},
{"key":["Mark",4,"2009/06/12 00:00:00"],"value":[34,33,50]},
{"key":["Mark",4,"2009/06/11 00:00:00"],"value":[32,29,51]},
{"key":["Mark",4,"2009/06/10 00:00:00"],"value":[20,20,45]}
]}

but if we use
group=true&descending=true&startkey=["Mark",4]&endkey=["Mark",4,{}]

we get
{"rows":[]}

and also
group=true&startkey=["Mark",4,{}]&endkey=["Mark",4]

also gives us
{"rows":[]}

Its a range, but since the range is everything that can be under a
key, it works for selecting everything selecting under a point in a
key array, its all good :)



On Thu, Jun 18, 2009 at 11:04 AM, Ross Bates<rb...@gmail.com> wrote:
> The syntax I'm using is something I picked up on the mailing list. It's
> supposed to allow you to select according the first value in the key array.
> I'm not using ranges or anything for the key. So if all docs look like this
>
> [foo, 123]
> [foo, 456]
> [bar, 123]
>
> ?startkey=[%22foo%22]&endkey=[%22foo%22,{}]<http://couch:5984/msg_db/_view/views/msg_by_tag_date?startkey=%5B%22Tag%22%5D&endkey=%5B%22Tag%22,%7B%7D%5D>
>
> would return this
>
> [foo, 123]
> [foo, 456]
>
>
>
>
>
>
> On Wed, Jun 17, 2009 at 5:58 PM, Blair Nilsson <bl...@gmail.com>wrote:
>
>> Maybe your start key and end key are around the wrong way for descending
>> order?
>>
>> On Thu, Jun 18, 2009 at 10:46 AM, Ross Bates<rb...@gmail.com> wrote:
>> > Hi All - I have a view that uses an array for it's key. I want to select
>> by
>> > the first value and sort by the second in descending order (it sorts
>> > properly in ascending order by default)
>> >
>> > I'm using this syntax to select the rows and everything works fine
>> >
>> >
>> http://couch:5984/msg_db/_view/views/msg_by_tag_date?startkey=[%22Tag%22]&endkey=[%22Tag%22,{}]<http://couch:5984/msg_db/_view/views/msg_by_tag_date?startkey=%5B%22Tag%22%5D&endkey=%5B%22Tag%22,%7B%7D%5D>
>> >
>> > but when I add the descending parameter like this:
>> >
>> >
>> http://couch:5984/msg_db/_view/views/msg_by_tag_date?descending=true&startkey=[%22Tag%22]&endkey=[%22Tag%22,{}]<http://couch:5984/msg_db/_view/views/msg_by_tag_date?descending=true&startkey=%5B%22Tag%22%5D&endkey=%5B%22Tag%22,%7B%7D%5D>
>> >
>> > couch.log indicates a 200 but I get 0 rows back.
>> >
>> > Is what am I trying to do possible?
>> >
>> > Thanks for any help!
>> >
>> > -Ross
>> >
>>
>

Re: startkey/endkey and descending=true

Posted by Ross Bates <rb...@gmail.com>.
The syntax I'm using is something I picked up on the mailing list. It's
supposed to allow you to select according the first value in the key array.
I'm not using ranges or anything for the key. So if all docs look like this

[foo, 123]
[foo, 456]
[bar, 123]

?startkey=[%22foo%22]&endkey=[%22foo%22,{}]<http://couch:5984/msg_db/_view/views/msg_by_tag_date?startkey=%5B%22Tag%22%5D&endkey=%5B%22Tag%22,%7B%7D%5D>

would return this

[foo, 123]
[foo, 456]






On Wed, Jun 17, 2009 at 5:58 PM, Blair Nilsson <bl...@gmail.com>wrote:

> Maybe your start key and end key are around the wrong way for descending
> order?
>
> On Thu, Jun 18, 2009 at 10:46 AM, Ross Bates<rb...@gmail.com> wrote:
> > Hi All - I have a view that uses an array for it's key. I want to select
> by
> > the first value and sort by the second in descending order (it sorts
> > properly in ascending order by default)
> >
> > I'm using this syntax to select the rows and everything works fine
> >
> >
> http://couch:5984/msg_db/_view/views/msg_by_tag_date?startkey=[%22Tag%22]&endkey=[%22Tag%22,{}]<http://couch:5984/msg_db/_view/views/msg_by_tag_date?startkey=%5B%22Tag%22%5D&endkey=%5B%22Tag%22,%7B%7D%5D>
> >
> > but when I add the descending parameter like this:
> >
> >
> http://couch:5984/msg_db/_view/views/msg_by_tag_date?descending=true&startkey=[%22Tag%22]&endkey=[%22Tag%22,{}]<http://couch:5984/msg_db/_view/views/msg_by_tag_date?descending=true&startkey=%5B%22Tag%22%5D&endkey=%5B%22Tag%22,%7B%7D%5D>
> >
> > couch.log indicates a 200 but I get 0 rows back.
> >
> > Is what am I trying to do possible?
> >
> > Thanks for any help!
> >
> > -Ross
> >
>

Re: startkey/endkey and descending=true

Posted by Blair Nilsson <bl...@gmail.com>.
Maybe your start key and end key are around the wrong way for descending order?

On Thu, Jun 18, 2009 at 10:46 AM, Ross Bates<rb...@gmail.com> wrote:
> Hi All - I have a view that uses an array for it's key. I want to select by
> the first value and sort by the second in descending order (it sorts
> properly in ascending order by default)
>
> I'm using this syntax to select the rows and everything works fine
>
> http://couch:5984/msg_db/_view/views/msg_by_tag_date?startkey=[%22Tag%22]&endkey=[%22Tag%22,{}]
>
> but when I add the descending parameter like this:
>
> http://couch:5984/msg_db/_view/views/msg_by_tag_date?descending=true&startkey=[%22Tag%22]&endkey=[%22Tag%22,{}]
>
> couch.log indicates a 200 but I get 0 rows back.
>
> Is what am I trying to do possible?
>
> Thanks for any help!
>
> -Ross
>