You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by "Mclendon, William C III" <wc...@sandia.gov> on 2011/03/29 20:39:10 UTC
Querying a view with a complex key with a wildcard
Hi,
I've got a view that is built from a map.js that looks like this:
function(doc)
{
if(doc['record type']=='search')
{
emit( [doc.status, doc.time], doc )
}
}
Where the doc.status field is a string that is either "Running" or "Finished" and doc.time is a floating point value from 0 and up representing the time a task took to complete.
I'd like to be able to query the documents that are 'Finished' and sort by time. This query works:
... /_view/searches_by_status?startkey=["Finished",0]&include_docs=true
But it doesn't sort… So if I change it to:
... /_view/searches_by_status?startkey=["Finished",0]&include_docs=true&descending=true
Then I only get values containing time=0
Why does adding the descending=true option change the results?
I've also tried this:
… /_view/searches_by_status?startkey=["Finished",0]&endkey=["Finished",9999.9999]&include_docs=true&descending=true
But this one doesn't return anything.
Is there a way to query this view to get records with a particular value in the doc.status field and still sort the fields?
Thanks in advance for any advice and/or help!
-William
Re: Querying a view with a complex key with a wildcard
Posted by Nathan Vander Wilt <na...@calftrail.com>.
On Mar 29, 2011, at 8:39 PM, Mclendon, William C III wrote:
> Hi,
>
> I've got a view that is built from a map.js that looks like this:
>
> function(doc)
> {
> if(doc['record type']=='search')
> {
> emit( [doc.status, doc.time], doc )
> }
> }
Not related to the main discussion, but:
You may want to avoid emitting the whole doc as the value for the key, and just simply emit([doc.status, doc.time]) — especially if you are using include_docs=true on your query anyway, emitting the document as the value for a key will just bloat your view (and when you query you'll get two copies sent over the wire too).
-nvw
Re: Querying a view with a complex key with a wildcard
Posted by Mehdi El Fadil <me...@mango-is.com>.
hi,
You can have a look at the section "View \ *Reversed result*" from the
couchdb guide to understand why it did not work with your start key and
descending order: http://guide.couchdb.org/editions/1/en/views.html
Good luck,
mehdi
On Tue, Mar 29, 2011 at 9:17 PM, Mclendon, William C III <wcmclen@sandia.gov
> wrote:
>
>
> On 3/29/11 12:43 PM, "Paul Davis" <pa...@gmail.com> wrote:
>
> >On Tue, Mar 29, 2011 at 2:39 PM, Mclendon, William C III
> ><wc...@sandia.gov> wrote:
> >> Hi,
> >>
> >> I've got a view that is built from a map.js that looks like this:
> >>
> >> function(doc)
> >> {
> >> if(doc['record type']=='search')
> >> {
> >> emit( [doc.status, doc.time], doc )
> >> }
> >> }
> >>
> >> Where the doc.status field is a string that is either "Running" or
> >>"Finished" and doc.time is a floating point value from 0 and up
> >>representing the time a task took to complete.
> >>
> >> I'd like to be able to query the documents that are 'Finished' and sort
> >>by time. This query works:
> >>
> >> ... /_view/searches_by_status?startkey=["Finished",0]&include_docs=true
> >>
> >> But it doesn't sortŠ So if I change it to:
> >>
> >
> >How do you mean, doesn't sort? I assure you its sorted, but most
> >likely just not sorted as you expect.
>
> Well, yea... it's in ascending order be default since descending=false is
> the default. I'd like descending order so my keys would be in an ordering
> something like this:
>
> ["Finished", 83.2]
> ["Finished", 77.3]
> ["Finished", 43.2]
> ...
> ["Finished", 0]
>
> If I run the view in futon, I can change the ordering of the key field in
> the table and see the right thing but I'd like to get this out of my view
> query through the URI as well.
>
>
> >> ...
> >>/_view/searches_by_status?startkey=["Finished",0]&include_docs=true&desce
> >>nding=true
> >>
> >> Then I only get values containing time=0
> >>
> >> Why does adding the descending=true option change the results?
> >>
> >> I've also tried this:
> >>
> >> Š
> >>/_view/searches_by_status?startkey=["Finished",0]&endkey=["Finished",9999
> >>.9999]&include_docs=true&descending=true
> >>
> >> But this one doesn't return anything.
> >>
> >> Is there a way to query this view to get records with a particular
> >>value in the doc.status field and still sort the fields?
> >>
> >
> >/_view/searches_by_status?startkey=["Finished"]&endkey=["Finished",{}]&inc
> >lude_docs=true
> >
> >This will return rows sorted by doc.time in ascending order.
> >
> >/_view/searches_by_status?startkey=["Finished",{}]&endkey=["Finished"]&inc
> >lude_docs=true&descending=true
> >
> >Will return docs sorted descending by doc.time.
>
> Ah, this works!
>
> I'm sure it's obvious that I'm fairly new to couchdb... Where might I go
> to look up what's happening with the {} values in the key? I've picked
> through the couchdb wiki a bit and have a couple couchdb books but haven't
> run across an explanation of this info in my searches :(
>
> Thanks!
>
>
> >> Thanks in advance for any advice and/or help!
> >> -William
> >>
> >
>
Re: Querying a view with a complex key with a wildcard
Posted by Paul Davis <pa...@gmail.com>.
> Where might I go to look up what's happening with the {} values in the key?
http://wiki.apache.org/couchdb/View_collation
All its doing is assuming that you don't have any objects in the
second array position. {} is just an empty object that sorts after
most things.
Re: Querying a view with a complex key with a wildcard
Posted by "Mclendon, William C III" <wc...@sandia.gov>.
On 3/29/11 12:43 PM, "Paul Davis" <pa...@gmail.com> wrote:
>On Tue, Mar 29, 2011 at 2:39 PM, Mclendon, William C III
><wc...@sandia.gov> wrote:
>> Hi,
>>
>> I've got a view that is built from a map.js that looks like this:
>>
>> function(doc)
>> {
>> if(doc['record type']=='search')
>> {
>> emit( [doc.status, doc.time], doc )
>> }
>> }
>>
>> Where the doc.status field is a string that is either "Running" or
>>"Finished" and doc.time is a floating point value from 0 and up
>>representing the time a task took to complete.
>>
>> I'd like to be able to query the documents that are 'Finished' and sort
>>by time. This query works:
>>
>> ... /_view/searches_by_status?startkey=["Finished",0]&include_docs=true
>>
>> But it doesn't sortŠ So if I change it to:
>>
>
>How do you mean, doesn't sort? I assure you its sorted, but most
>likely just not sorted as you expect.
Well, yea... it's in ascending order be default since descending=false is
the default. I'd like descending order so my keys would be in an ordering
something like this:
["Finished", 83.2]
["Finished", 77.3]
["Finished", 43.2]
...
["Finished", 0]
If I run the view in futon, I can change the ordering of the key field in
the table and see the right thing but I'd like to get this out of my view
query through the URI as well.
>> ...
>>/_view/searches_by_status?startkey=["Finished",0]&include_docs=true&desce
>>nding=true
>>
>> Then I only get values containing time=0
>>
>> Why does adding the descending=true option change the results?
>>
>> I've also tried this:
>>
>> Š
>>/_view/searches_by_status?startkey=["Finished",0]&endkey=["Finished",9999
>>.9999]&include_docs=true&descending=true
>>
>> But this one doesn't return anything.
>>
>> Is there a way to query this view to get records with a particular
>>value in the doc.status field and still sort the fields?
>>
>
>/_view/searches_by_status?startkey=["Finished"]&endkey=["Finished",{}]&inc
>lude_docs=true
>
>This will return rows sorted by doc.time in ascending order.
>
>/_view/searches_by_status?startkey=["Finished",{}]&endkey=["Finished"]&inc
>lude_docs=true&descending=true
>
>Will return docs sorted descending by doc.time.
Ah, this works!
I'm sure it's obvious that I'm fairly new to couchdb... Where might I go
to look up what's happening with the {} values in the key? I've picked
through the couchdb wiki a bit and have a couple couchdb books but haven't
run across an explanation of this info in my searches :(
Thanks!
>> Thanks in advance for any advice and/or help!
>> -William
>>
>
Re: Querying a view with a complex key with a wildcard
Posted by Paul Davis <pa...@gmail.com>.
On Tue, Mar 29, 2011 at 2:39 PM, Mclendon, William C III
<wc...@sandia.gov> wrote:
> Hi,
>
> I've got a view that is built from a map.js that looks like this:
>
> function(doc)
> {
> if(doc['record type']=='search')
> {
> emit( [doc.status, doc.time], doc )
> }
> }
>
> Where the doc.status field is a string that is either "Running" or "Finished" and doc.time is a floating point value from 0 and up representing the time a task took to complete.
>
> I'd like to be able to query the documents that are 'Finished' and sort by time. This query works:
>
> ... /_view/searches_by_status?startkey=["Finished",0]&include_docs=true
>
> But it doesn't sort… So if I change it to:
>
How do you mean, doesn't sort? I assure you its sorted, but most
likely just not sorted as you expect.
> ... /_view/searches_by_status?startkey=["Finished",0]&include_docs=true&descending=true
>
> Then I only get values containing time=0
>
> Why does adding the descending=true option change the results?
>
> I've also tried this:
>
> … /_view/searches_by_status?startkey=["Finished",0]&endkey=["Finished",9999.9999]&include_docs=true&descending=true
>
> But this one doesn't return anything.
>
> Is there a way to query this view to get records with a particular value in the doc.status field and still sort the fields?
>
/_view/searches_by_status?startkey=["Finished"]&endkey=["Finished",{}]&include_docs=true
This will return rows sorted by doc.time in ascending order.
/_view/searches_by_status?startkey=["Finished",{}]&endkey=["Finished"]&include_docs=true&descending=true
Will return docs sorted descending by doc.time.
> Thanks in advance for any advice and/or help!
> -William
>