You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by madhav sharma <ma...@gmail.com> on 2010/02/10 06:44:30 UTC

parametrized view generation

hi everyone
I am couchdb for one of my project and i am stuck at a place where i need
your help.
My use case of problem is that i have a set of documents which have same
field with different value and i need to get the value of  field (say name)
from a document with id (say information) , and i want this mapfunction to
be generic so that i can call this with different document id and get the
the field value out of it.
In sql it can just be(  select name from Table where id = 1233 )  so any
help is appreciated

Thanks

Re: parametrized view generation

Posted by madhav sharma <ma...@gmail.com>.
Thanks Brian
your suggestions really guided  me and i was able to get my code working .
sorry for delay in reply :) .
regards

Madhav

Re: parametrized view generation

Posted by Brian Candler <B....@pobox.com>.
On Wed, Feb 10, 2010 at 11:14:30AM +0530, madhav sharma wrote:
> I am couchdb for one of my project and i am stuck at a place where i need
> your help.
> My use case of problem is that i have a set of documents which have same
> field with different value and i need to get the value of  field (say name)
> from a document with id (say information) , and i want this mapfunction to
> be generic so that i can call this with different document id and get the
> the field value out of it.
> In sql it can just be(  select name from Table where id = 1233 )  so any
> help is appreciated

In that particular case, you could just GET the document (since you use the
doc._id to GET a document)

select * from docs where id = 123
  => GET /dbname/123

select * from docs where id in (123,456)
  => POST /dbname/_all_docs?include_docs=true
     {"keys":["123","456"]}

If you're searching a field other than doc._id, then you make a map
function.

function(doc) {
  if (doc.name) {
    emit(doc.name, null);
  }
}

select * from docs where name = 'foo'
  => GET /dbname/_design/mydesign/_view/view1?key="foo"&include_docs=true

If you leave out include_docs=true then you will get the doc id, the emitted
key and the emitted value (here null), but not the rest of the doc.

If you always want to retrieve some other values from the doc, you can
include it in the value part, which is more efficient than fetching the
whole doc.  For example

function(doc) {
  if (doc.name) {
    emit(doc.name, doc.description || null);
  }
}

select description from docs where name = 'foo'
  => GET /dbname/_design/mydesign/_view/view2?key="foo"

But you can be cleverer, and do things which are much harder in SQL.  For
example, you can index all fields by value:

function(doc) {
  for (var k in doc) {
    emit(doc[k], k);
  }
}

select id from docs where name='foo' or description='foo' or label='foo' or...
  => GET /dbname/_design/mydesign/_view/view3?key="foo"

The result row will give you the match as the key, and the field it came
from as the value.

Or you can have a single index which lets you search on any specific field.

function(doc) {
  for (var k in doc) {
    emit([k, doc[k]], null);
  }
}

select id from docs where description='foo'
  => GET /dbname/_design/mydesign/_view/view4?key=["description","foo"]

Using startkey/endkey you can do searching for ranges and prefixes.

Hope this gives you a starting point.

Brian.

Re: parametrized view generation

Posted by madhav sharma <ma...@gmail.com>.
Thanks guys for quick replies.
Actually i am using scala as programming language and scouchdb as a
interface to communicate with couchdb.
I will try the solutions suggested by you people and get back soon .
Regards
Madhav

Re: parametrized view generation

Posted by Mano <ma...@gmail.com>.
On Wed, Feb 10, 2010 at 11:50 AM, Rodd Snook <ro...@gmail.com> wrote:

> I don't think that's going to do what the OP wants, which is picking a
> particular field out of the document. It's also going to create a huge
> view, because it contains the whole document. I think what he's
> looking for is:
>
> emit(doc._id, doc.name)
>
>
You are right. I did not understand his requirements properly!

regds,
mano

Re: parametrized view generation

Posted by Rodd Snook <ro...@gmail.com>.
On Wed, Feb 10, 2010 at 4:56 PM, Mano <ma...@gmail.com> wrote:
>
> On Wed, Feb 10, 2010 at 11:14 AM, madhav sharma <mail.madhavsharma@gmail.com
> > wrote:
>
> > hi everyone
> > I am couchdb for one of my project and i am stuck at a place where i need
> > your help.
> > My use case of problem is that i have a set of documents which have same
> > field with different value and i need to get the value of  field (say name)
> > from a document with id (say information) , and i want this mapfunction to
> > be generic so that i can call this with different document id and get the
> > the field value out of it.
> > In sql it can just be(  select name from Table where id = 1233 )  so any
> > help is appreciated
> >
>
> The map function can:
>
> emit( [doc.name, doc._id], doc).
>
> You can query this view with a 'key' of [name, 1233].
>
> regds,
> mano

I don't think that's going to do what the OP wants, which is picking a
particular field out of the document. It's also going to create a huge
view, because it contains the whole document. I think what he's
looking for is:

emit(doc._id, doc.name)

[disclaimer: I'm totally new at CouchDb too]

Re: parametrized view generation

Posted by Mano <ma...@gmail.com>.
On Wed, Feb 10, 2010 at 11:14 AM, madhav sharma <mail.madhavsharma@gmail.com
> wrote:

> hi everyone
> I am couchdb for one of my project and i am stuck at a place where i need
> your help.
> My use case of problem is that i have a set of documents which have same
> field with different value and i need to get the value of  field (say name)
> from a document with id (say information) , and i want this mapfunction to
> be generic so that i can call this with different document id and get the
> the field value out of it.
> In sql it can just be(  select name from Table where id = 1233 )  so any
> help is appreciated
>

The map function can:

emit( [doc.name, doc._id], doc).

You can query this view with a 'key' of [name, 1233].

regds,
mano

Re: parametrized view generation

Posted by Patrick Barnes <mr...@gmail.com>.
Hi madhav,

Typically, you create a view where the key is the data you want to 
filter on, and the value is the data you want to retrieve.

eg: If you have a big db of people, and you want to be able to look up 
people by their phone number, and will want to display at least their names:

map = function(doc) { emit(doc.phone, doc.name); }

To find the name of a person who has the phone number 12345678, send a 
request to: 
http://localhost:5984/dbpeople/_design/dbpeople/_view/by_phone_number?key=12345678

If you want to see more than just their name:
http://localhost:5984/dbpeople/_design/dbpeople/_view/by_phone_number?key=12345678&include_docs=true
(the include_docs parameter will include the entire document in the 
returned data.)





On 10/02/2010 4:44 PM, madhav sharma wrote:
> hi everyone
> I am couchdb for one of my project and i am stuck at a place where i need
> your help.
> My use case of problem is that i have a set of documents which have same
> field with different value and i need to get the value of  field (say name)
> from a document with id (say information) , and i want this mapfunction to
> be generic so that i can call this with different document id and get the
> the field value out of it.
> In sql it can just be(  select name from Table where id = 1233 )  so any
> help is appreciated
>
> Thanks
>