You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Bernd Eickhoff <me...@mymonster.net> on 2008/12/28 22:06:16 UTC

select where

Hello,

I am trying to get to grips with Couchdb. Though it looks very  
interesting, I just dont understand how to query Couchdb for an  
abitrary value, as is possible with sql. Imagine I had a number of  
very simple documents, like:

{"_id":"b1","_rev":"3573740128", "book":"Potter","author":"Rowling"}

and I wanted to find all books written by Mr Tolkien?

I could create a view like:

function(doc) { if (doc.author == "Tolkien") emit(null, doc); }

and then query:

http://127.0.0.1:5984/books/_view/byauthor/tolkien

Is there any way to pass "Tolkien" as a variable to the view, or do I  
have to create a new view, every time I want to query for another  
keyword?

Thanks

Bernd

Re: select where

Posted by Chris Anderson <jc...@gmail.com>.
On Wed, Jan 7, 2009 at 12:03 AM, paul jobs <we...@gmail.com> wrote:
> function(d) {
> if (d.type!="reply")
> emit([d.fromuid,d.touid,d.replycount],d);
> }
>
> is it possible to use this map function
> to prepare views
>
> like fromuid ==uid or touid ==uid and d.replycount>0 using couchdb view keys

No, you've got to remember to that all a map function does is build a
sorted list. All queries do is pull out sections of that list. So you
can have a list sorted by first name, or a list sorted by last name,
or a sorted by first, last, or a list sorted by last, first.

If you need to query by first, last as well as last, first -- you're
going to need 2 views.

Hope that helps

-- 
Chris Anderson
http://jchris.mfdz.com

Re: select where

Posted by paul jobs <we...@gmail.com>.
function(d) {
if (d.type!="reply")
emit([d.fromuid,d.touid,d.replycount],d);
}

is it possible to use this map function
to prepare views

like fromuid ==uid or touid ==uid and d.replycount>0 using couchdb view keys

On 1/6/09, paul jobs <we...@gmail.com> wrote:
>
> Nanodeath on couchdb irc solved this ->
> function(d){
>   if(d.type != 'reply'){
>     emit(d.touid, d);
>     if(d.touid != d.fromuid){
>       if (d.replycount>0)
>       emit(d.fromuid, d);
>     }
>   }
> }
>
>
>
> Usage: In [7]: replies = [ (x.id, x.value) for x in
> anondb.view('_view/truthbox/inbox',key=z.uid) ]
>
> Schema:
>
> class Anonmessage(Document):
>     subject = TextField()
>     read = BooleanField()
>     fromread = BooleanField()
>     toread = BooleanField()
>     msg = TextField()
>     background = TextField()
>     type = TextField()
>     reply = TextField()
>     fromuid = LongField()
>     replycount = IntegerField()
>     touid = LongField()
>     created = DateTimeField(default=datetime.datetime.now())
>     time =TimeField(default=datetime.datetime.now())
>     date = DateField(default=datetime.date.today())
>
>
>
>
>
> On 1/6/09, paul jobs <we...@gmail.com> wrote:
>>
>> querycode  = 'function(d) { if ((d.touid == "%d" || d.fromuid=="%d") &&
>> d.type!="reply" ) emit(d.created,d); }'%(uid, uid)
>> anonmsgs = [ (x.id,x.value) for x in varnishanondb.query(querycode,
>> descending=True) ]
>>
>> how to use this using slices -> when there are 2 variables in the query
>> instead of just d.author
>>
>> On 12/28/08, Paul Davis <pa...@gmail.com> wrote:
>>>
>>> Behold the power of slices!
>>>
>>> function(doc)
>>> {
>>>     if(doc.author) emit(doc.author, 1);
>>> }
>>>
>>> All docs that have Tolkien as an author:
>>>
>>> http://127.0.0.1:5984/db_name/_view/design_doc/foo?key="Tolkien"
>>>
>>> Obviously, s/Tolkien/Rowling/ for books by Rowling.
>>>
>>> The more important part is this though, say you wanted all books with
>>> authors alphabetically from Rowling to Tolkein:
>>>
>>> http://127.0.0.1:5984/db_name/_view/design_doc/foor?startkey=
>>> "Rowling"&andkey="Tolkien"
>>>
>>> And remember, you can emit arbitrary JSON. There's a defined sort
>>> order over the entire range of JSON. See the View collation wiki page.
>>>
>>> HTH,
>>>
>>> Paul Davis
>>>
>>>
>>> On Sun, Dec 28, 2008 at 4:06 PM, Bernd Eickhoff <me...@mymonster.net>
>>> wrote:
>>> > Hello,
>>> >
>>> > I am trying to get to grips with Couchdb. Though it looks very
>>> interesting,
>>> > I just dont understand how to query Couchdb for an abitrary value, as
>>> is
>>> > possible with sql. Imagine I had a number of very simple documents,
>>> like:
>>> >
>>> > {"_id":"b1","_rev":"3573740128", "book":"Potter","author":"Rowling"}
>>> >
>>> > and I wanted to find all books written by Mr Tolkien?
>>> >
>>> > I could create a view like:
>>> >
>>> > function(doc) { if (doc.author == "Tolkien") emit(null, doc); }
>>> >
>>> > and then query:
>>> >
>>> > http://127.0.0.1:5984/books/_view/byauthor/tolkien
>>> >
>>> > Is there any way to pass "Tolkien" as a variable to the view, or do I
>>> have
>>> > to create a new view, every time I want to query for another keyword?
>>> >
>>> > Thanks
>>> >
>>> > Bernd
>>> >
>>>
>>
>>
>

Re: select where

Posted by paul jobs <we...@gmail.com>.
Nanodeath on couchdb irc solved this ->
function(d){
  if(d.type != 'reply'){
    emit(d.touid, d);
    if(d.touid != d.fromuid){
      if (d.replycount>0)
      emit(d.fromuid, d);
    }
  }
}



Usage: In [7]: replies = [ (x.id, x.value) for x in
anondb.view('_view/truthbox/inbox',key=z.uid) ]

Schema:

class Anonmessage(Document):
    subject = TextField()
    read = BooleanField()
    fromread = BooleanField()
    toread = BooleanField()
    msg = TextField()
    background = TextField()
    type = TextField()
    reply = TextField()
    fromuid = LongField()
    replycount = IntegerField()
    touid = LongField()
    created = DateTimeField(default=datetime.datetime.now())
    time =TimeField(default=datetime.datetime.now())
    date = DateField(default=datetime.date.today())





On 1/6/09, paul jobs <we...@gmail.com> wrote:
>
> querycode  = 'function(d) { if ((d.touid == "%d" || d.fromuid=="%d") &&
> d.type!="reply" ) emit(d.created,d); }'%(uid, uid)
> anonmsgs = [ (x.id,x.value) for x in varnishanondb.query(querycode,
> descending=True) ]
>
> how to use this using slices -> when there are 2 variables in the query
> instead of just d.author
>
> On 12/28/08, Paul Davis <pa...@gmail.com> wrote:
>>
>> Behold the power of slices!
>>
>> function(doc)
>> {
>>     if(doc.author) emit(doc.author, 1);
>> }
>>
>> All docs that have Tolkien as an author:
>>
>> http://127.0.0.1:5984/db_name/_view/design_doc/foo?key="Tolkien"
>>
>> Obviously, s/Tolkien/Rowling/ for books by Rowling.
>>
>> The more important part is this though, say you wanted all books with
>> authors alphabetically from Rowling to Tolkein:
>>
>> http://127.0.0.1:5984/db_name/_view/design_doc/foor?startkey=
>> "Rowling"&andkey="Tolkien"
>>
>> And remember, you can emit arbitrary JSON. There's a defined sort
>> order over the entire range of JSON. See the View collation wiki page.
>>
>> HTH,
>>
>> Paul Davis
>>
>>
>> On Sun, Dec 28, 2008 at 4:06 PM, Bernd Eickhoff <me...@mymonster.net> wrote:
>> > Hello,
>> >
>> > I am trying to get to grips with Couchdb. Though it looks very
>> interesting,
>> > I just dont understand how to query Couchdb for an abitrary value, as is
>> > possible with sql. Imagine I had a number of very simple documents,
>> like:
>> >
>> > {"_id":"b1","_rev":"3573740128", "book":"Potter","author":"Rowling"}
>> >
>> > and I wanted to find all books written by Mr Tolkien?
>> >
>> > I could create a view like:
>> >
>> > function(doc) { if (doc.author == "Tolkien") emit(null, doc); }
>> >
>> > and then query:
>> >
>> > http://127.0.0.1:5984/books/_view/byauthor/tolkien
>> >
>> > Is there any way to pass "Tolkien" as a variable to the view, or do I
>> have
>> > to create a new view, every time I want to query for another keyword?
>> >
>> > Thanks
>> >
>> > Bernd
>> >
>>
>
>

Re: select where

Posted by paul jobs <we...@gmail.com>.
querycode  = 'function(d) { if ((d.touid == "%d" || d.fromuid=="%d") &&
d.type!="reply" ) emit(d.created,d); }'%(uid, uid)
anonmsgs = [ (x.id,x.value) for x in varnishanondb.query(querycode,
descending=True) ]

how to use this using slices -> when there are 2 variables in the query
instead of just d.author

On 12/28/08, Paul Davis <pa...@gmail.com> wrote:
>
> Behold the power of slices!
>
> function(doc)
> {
>     if(doc.author) emit(doc.author, 1);
> }
>
> All docs that have Tolkien as an author:
>
> http://127.0.0.1:5984/db_name/_view/design_doc/foo?key="Tolkien"
>
> Obviously, s/Tolkien/Rowling/ for books by Rowling.
>
> The more important part is this though, say you wanted all books with
> authors alphabetically from Rowling to Tolkein:
>
> http://127.0.0.1:5984/db_name/_view/design_doc/foor?startkey=
> "Rowling"&andkey="Tolkien"
>
> And remember, you can emit arbitrary JSON. There's a defined sort
> order over the entire range of JSON. See the View collation wiki page.
>
> HTH,
>
> Paul Davis
>
>
> On Sun, Dec 28, 2008 at 4:06 PM, Bernd Eickhoff <me...@mymonster.net> wrote:
> > Hello,
> >
> > I am trying to get to grips with Couchdb. Though it looks very
> interesting,
> > I just dont understand how to query Couchdb for an abitrary value, as is
> > possible with sql. Imagine I had a number of very simple documents, like:
> >
> > {"_id":"b1","_rev":"3573740128", "book":"Potter","author":"Rowling"}
> >
> > and I wanted to find all books written by Mr Tolkien?
> >
> > I could create a view like:
> >
> > function(doc) { if (doc.author == "Tolkien") emit(null, doc); }
> >
> > and then query:
> >
> > http://127.0.0.1:5984/books/_view/byauthor/tolkien
> >
> > Is there any way to pass "Tolkien" as a variable to the view, or do I
> have
> > to create a new view, every time I want to query for another keyword?
> >
> > Thanks
> >
> > Bernd
> >
>

Re: select where

Posted by Paul Carey <pa...@gmail.com>.
> http://127.0.0.1:5984/books/_view/query/byAuthor?key=Tolkien
> returns an error {"error":"EXIT"...
> what's going wrong?

Bernd

Not sure if it's the cause of your error, but you'll need to URL
encode a JSON encoded key, so Tolkien becomes "Tolkien" becomes
%22Tolkien%22

http://127.0.0.1:5984/books/_view/query/byAuthor?key=%22Tolkien%22

Paul

Re: select where

Posted by Bernd Eickhoff <me...@mymonster.net>.
Thanks for your reply, but somehow it does not work for me. I created  
the view you described.

http://127.0.0.1:5984/books/_view/query/byAuthor

returns all objects. But

http://127.0.0.1:5984/books/_view/query/byAuthor?key=Tolkien

returns an error {"error":"EXIT"...

what's going wrong?

Thanks,

Bernd Eickhoff
Am 28.12.2008 um 22:15 schrieb Paul Davis:

> Behold the power of slices!
>
> function(doc)
> {
>    if(doc.author) emit(doc.author, 1);
> }
>
> All docs that have Tolkien as an author:
>
> http://127.0.0.1:5984/db_name/_view/design_doc/foo?key="Tolkien"
>
> Obviously, s/Tolkien/Rowling/ for books by Rowling.
>
> The more important part is this though, say you wanted all books with
> authors alphabetically from Rowling to Tolkein:
>
> http://127.0.0.1:5984/db_name/_view/design_doc/foor? 
> startkey="Rowling"&andkey="Tolkien"
>
> And remember, you can emit arbitrary JSON. There's a defined sort
> order over the entire range of JSON. See the View collation wiki page.
>
> HTH,
> Paul Davis
>
> On Sun, Dec 28, 2008 at 4:06 PM, Bernd Eickhoff <me...@mymonster.net>  
> wrote:
>> Hello,
>>
>> I am trying to get to grips with Couchdb. Though it looks very  
>> interesting,
>> I just dont understand how to query Couchdb for an abitrary value,  
>> as is
>> possible with sql. Imagine I had a number of very simple documents,  
>> like:
>>
>> {"_id":"b1","_rev":"3573740128", "book":"Potter","author":"Rowling"}
>>
>> and I wanted to find all books written by Mr Tolkien?
>>
>> I could create a view like:
>>
>> function(doc) { if (doc.author == "Tolkien") emit(null, doc); }
>>
>> and then query:
>>
>> http://127.0.0.1:5984/books/_view/byauthor/tolkien
>>
>> Is there any way to pass "Tolkien" as a variable to the view, or do  
>> I have
>> to create a new view, every time I want to query for another keyword?
>>
>> Thanks
>>
>> Bernd
>>


Re: select where

Posted by Paul Davis <pa...@gmail.com>.
Behold the power of slices!

function(doc)
{
    if(doc.author) emit(doc.author, 1);
}

All docs that have Tolkien as an author:

http://127.0.0.1:5984/db_name/_view/design_doc/foo?key="Tolkien"

Obviously, s/Tolkien/Rowling/ for books by Rowling.

The more important part is this though, say you wanted all books with
authors alphabetically from Rowling to Tolkein:

http://127.0.0.1:5984/db_name/_view/design_doc/foor?startkey="Rowling"&andkey="Tolkien"

And remember, you can emit arbitrary JSON. There's a defined sort
order over the entire range of JSON. See the View collation wiki page.

HTH,
Paul Davis

On Sun, Dec 28, 2008 at 4:06 PM, Bernd Eickhoff <me...@mymonster.net> wrote:
> Hello,
>
> I am trying to get to grips with Couchdb. Though it looks very interesting,
> I just dont understand how to query Couchdb for an abitrary value, as is
> possible with sql. Imagine I had a number of very simple documents, like:
>
> {"_id":"b1","_rev":"3573740128", "book":"Potter","author":"Rowling"}
>
> and I wanted to find all books written by Mr Tolkien?
>
> I could create a view like:
>
> function(doc) { if (doc.author == "Tolkien") emit(null, doc); }
>
> and then query:
>
> http://127.0.0.1:5984/books/_view/byauthor/tolkien
>
> Is there any way to pass "Tolkien" as a variable to the view, or do I have
> to create a new view, every time I want to query for another keyword?
>
> Thanks
>
> Bernd
>