You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Daniel DeLeo <de...@gmail.com> on 2008/12/18 17:41:28 UTC

Adding attributes to a document in the view?

Is it possible in the newer versions to add attributes to a document object
in the view?  For example, on 0.8.0, if your view map function is:

function(doc) {
  doc.computed_attribute = 'hello, world';
  emit(doc._id, doc);
};

The view gives a 50X error, and a stack trace in futon.  Is it possible to
do this in any version of Couch?

Thanks,
Dan DeLeo

Re: Adding attributes to a document in the view?

Posted by Chris Anderson <jc...@gmail.com>.
On Thu, Dec 18, 2008 at 8:41 AM, Daniel DeLeo <de...@gmail.com> wrote:
> Is it possible in the newer versions to add attributes to a document object
> in the view?  For example, on 0.8.0, if your view map function is:
>
> function(doc) {
>  doc.computed_attribute = 'hello, world';
>  emit(doc._id, doc);
> };
>
> The view gives a 50X error, and a stack trace in futon.  Is it possible to
> do this in any version of Couch?

It is not recommended. You may be able to get away with it on new
versions of CouchDB (as Spidermonkey's freeze seems to have stopped
working for us).

A better solution would be:

function(doc) {
  var value = {
    computed_attribute : 'hello, world',
    another : doc.another
  }
  emit(doc._id, value)
}


This avoids the pitfall of changes to the doc leaking into unrelated
map function calls elsewhere in the design document.


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

Re: Adding attributes to a document in the view?

Posted by Daniel DeLeo <de...@gmail.com>.
What I'm looking at doing is adding some attributes to a doc in the view and
saving them via a round trip to a client (running on the same machine,
though).  docs that have already been updated would (of course) be excluded
from the view.  The point of this is that other views can operate on the
"extracted" data in the updated documents (removing the need for code
duplication).

I'm just investigating this as one possible approach--the obvious
alternative would be to process the data before putting it in couch in the
first place, but this has some disadvantages in my application, so I'm
trying this approach, too, in order to compare them.

Thanks for the tips and quick replies.

Dan DeLeo

On Thu, Dec 18, 2008 at 11:14 AM, Paul Davis <pa...@gmail.com>wrote:

> On Thu, Dec 18, 2008 at 12:09 PM, Daniel DeLeo <de...@gmail.com>
> wrote:
> > I wasn't expecting these attributes to be saved, just returned (emitted).
> > And I'll trust that docs are frozen before getting to the map function
> for
> > good reason.
>
> Just checking :)
>
> >
> > Paul, you said that adding an attribute in a view would prevent them from
> > being incrementally computed.  I am I right in thinking that you're
> > preempting any request for a feature like this (I wasn't planning on
> asking
> > for it)? Is there anything wrong with using a "deep copy" function as a
> > workaround?
> >
>
> Mostly right, just trying to explain why it probably wouldn't be a
> good feature instead of the usual "No, and won't happen" explanations
> that we geeks are famous for. I think the deep copy would be a fine
> answer, but as Chris says, do you really need an entire copy of the
> doc?
>
> > Thanks.
> >
> > On Thu, Dec 18, 2008 at 9:56 AM, Matthew Wilson <ma...@tplus1.com> wrote:
> >
> >> I'm still a couchdb rookie, and I don't want to sound condescending or
> >> obnoxious, but the term "view" suggests to me that it should be a
> >> read-only operation.
> >>
> >>
> >>
> >> On Thu, Dec 18, 2008 at 11:50 AM, Paul Davis
> >> <pa...@gmail.com> wrote:
> >> > What Chris said.  Also, if you're expecting any attributes you add to
> >> > the document to be saved to the database that'll never happen. Views
> >> > need to satisfy some properties to ensure that views can be
> >> > incrementally computed. Adding attributes in a view would be a sure
> >> > fire way to start breaking these requirements.
> >> >
> >> > Paul
> >> >
> >> > On Thu, Dec 18, 2008 at 11:41 AM, Daniel DeLeo <de...@gmail.com>
> >> wrote:
> >> >> Is it possible in the newer versions to add attributes to a document
> >> object
> >> >> in the view?  For example, on 0.8.0, if your view map function is:
> >> >>
> >> >> function(doc) {
> >> >>  doc.computed_attribute = 'hello, world';
> >> >>  emit(doc._id, doc);
> >> >> };
> >> >>
> >> >> The view gives a 50X error, and a stack trace in futon.  Is it
> possible
> >> to
> >> >> do this in any version of Couch?
> >> >>
> >> >> Thanks,
> >> >> Dan DeLeo
> >> >>
> >> >
> >>
> >>
> >>
> >> --
> >> Matthew Wilson
> >> matt@tplus1.com
> >> http://tplus1.com
> >>
> >
>

Re: Adding attributes to a document in the view?

Posted by Paul Davis <pa...@gmail.com>.
On Thu, Dec 18, 2008 at 12:09 PM, Daniel DeLeo <de...@gmail.com> wrote:
> I wasn't expecting these attributes to be saved, just returned (emitted).
> And I'll trust that docs are frozen before getting to the map function for
> good reason.

Just checking :)

>
> Paul, you said that adding an attribute in a view would prevent them from
> being incrementally computed.  I am I right in thinking that you're
> preempting any request for a feature like this (I wasn't planning on asking
> for it)? Is there anything wrong with using a "deep copy" function as a
> workaround?
>

Mostly right, just trying to explain why it probably wouldn't be a
good feature instead of the usual "No, and won't happen" explanations
that we geeks are famous for. I think the deep copy would be a fine
answer, but as Chris says, do you really need an entire copy of the
doc?

> Thanks.
>
> On Thu, Dec 18, 2008 at 9:56 AM, Matthew Wilson <ma...@tplus1.com> wrote:
>
>> I'm still a couchdb rookie, and I don't want to sound condescending or
>> obnoxious, but the term "view" suggests to me that it should be a
>> read-only operation.
>>
>>
>>
>> On Thu, Dec 18, 2008 at 11:50 AM, Paul Davis
>> <pa...@gmail.com> wrote:
>> > What Chris said.  Also, if you're expecting any attributes you add to
>> > the document to be saved to the database that'll never happen. Views
>> > need to satisfy some properties to ensure that views can be
>> > incrementally computed. Adding attributes in a view would be a sure
>> > fire way to start breaking these requirements.
>> >
>> > Paul
>> >
>> > On Thu, Dec 18, 2008 at 11:41 AM, Daniel DeLeo <de...@gmail.com>
>> wrote:
>> >> Is it possible in the newer versions to add attributes to a document
>> object
>> >> in the view?  For example, on 0.8.0, if your view map function is:
>> >>
>> >> function(doc) {
>> >>  doc.computed_attribute = 'hello, world';
>> >>  emit(doc._id, doc);
>> >> };
>> >>
>> >> The view gives a 50X error, and a stack trace in futon.  Is it possible
>> to
>> >> do this in any version of Couch?
>> >>
>> >> Thanks,
>> >> Dan DeLeo
>> >>
>> >
>>
>>
>>
>> --
>> Matthew Wilson
>> matt@tplus1.com
>> http://tplus1.com
>>
>

Re: Adding attributes to a document in the view?

Posted by Chris Anderson <jc...@gmail.com>.
On Thu, Dec 18, 2008 at 9:09 AM, Daniel DeLeo <de...@gmail.com> wrote:
> I wasn't expecting these attributes to be saved, just returned (emitted).
> And I'll trust that docs are frozen before getting to the map function for
> good reason.

Deep copy would work. Still I think a deep copy of only the necessary
fields (as in my example) is good practice. Rarely in your application
will you need the whole document in the value of a view. By emitting
only the necessary data you'll get better performance and I think
cleaner application design.

There is also the include_docs query option, which is useful if you
occasionally want to access the doc, but usually just need what's
available in the value. (If you really do nearly always need the whole
doc(s) on view-range queries, it may be better to continue to emit it
in the value...)

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

Re: Adding attributes to a document in the view?

Posted by Daniel DeLeo <de...@gmail.com>.
I wasn't expecting these attributes to be saved, just returned (emitted).
And I'll trust that docs are frozen before getting to the map function for
good reason.

Paul, you said that adding an attribute in a view would prevent them from
being incrementally computed.  I am I right in thinking that you're
preempting any request for a feature like this (I wasn't planning on asking
for it)? Is there anything wrong with using a "deep copy" function as a
workaround?

Thanks.

On Thu, Dec 18, 2008 at 9:56 AM, Matthew Wilson <ma...@tplus1.com> wrote:

> I'm still a couchdb rookie, and I don't want to sound condescending or
> obnoxious, but the term "view" suggests to me that it should be a
> read-only operation.
>
>
>
> On Thu, Dec 18, 2008 at 11:50 AM, Paul Davis
> <pa...@gmail.com> wrote:
> > What Chris said.  Also, if you're expecting any attributes you add to
> > the document to be saved to the database that'll never happen. Views
> > need to satisfy some properties to ensure that views can be
> > incrementally computed. Adding attributes in a view would be a sure
> > fire way to start breaking these requirements.
> >
> > Paul
> >
> > On Thu, Dec 18, 2008 at 11:41 AM, Daniel DeLeo <de...@gmail.com>
> wrote:
> >> Is it possible in the newer versions to add attributes to a document
> object
> >> in the view?  For example, on 0.8.0, if your view map function is:
> >>
> >> function(doc) {
> >>  doc.computed_attribute = 'hello, world';
> >>  emit(doc._id, doc);
> >> };
> >>
> >> The view gives a 50X error, and a stack trace in futon.  Is it possible
> to
> >> do this in any version of Couch?
> >>
> >> Thanks,
> >> Dan DeLeo
> >>
> >
>
>
>
> --
> Matthew Wilson
> matt@tplus1.com
> http://tplus1.com
>

Re: Adding attributes to a document in the view?

Posted by Matthew Wilson <ma...@tplus1.com>.
I'm still a couchdb rookie, and I don't want to sound condescending or
obnoxious, but the term "view" suggests to me that it should be a
read-only operation.



On Thu, Dec 18, 2008 at 11:50 AM, Paul Davis
<pa...@gmail.com> wrote:
> What Chris said.  Also, if you're expecting any attributes you add to
> the document to be saved to the database that'll never happen. Views
> need to satisfy some properties to ensure that views can be
> incrementally computed. Adding attributes in a view would be a sure
> fire way to start breaking these requirements.
>
> Paul
>
> On Thu, Dec 18, 2008 at 11:41 AM, Daniel DeLeo <de...@gmail.com> wrote:
>> Is it possible in the newer versions to add attributes to a document object
>> in the view?  For example, on 0.8.0, if your view map function is:
>>
>> function(doc) {
>>  doc.computed_attribute = 'hello, world';
>>  emit(doc._id, doc);
>> };
>>
>> The view gives a 50X error, and a stack trace in futon.  Is it possible to
>> do this in any version of Couch?
>>
>> Thanks,
>> Dan DeLeo
>>
>



-- 
Matthew Wilson
matt@tplus1.com
http://tplus1.com

Re: Adding attributes to a document in the view?

Posted by Paul Davis <pa...@gmail.com>.
What Chris said.  Also, if you're expecting any attributes you add to
the document to be saved to the database that'll never happen. Views
need to satisfy some properties to ensure that views can be
incrementally computed. Adding attributes in a view would be a sure
fire way to start breaking these requirements.

Paul

On Thu, Dec 18, 2008 at 11:41 AM, Daniel DeLeo <de...@gmail.com> wrote:
> Is it possible in the newer versions to add attributes to a document object
> in the view?  For example, on 0.8.0, if your view map function is:
>
> function(doc) {
>  doc.computed_attribute = 'hello, world';
>  emit(doc._id, doc);
> };
>
> The view gives a 50X error, and a stack trace in futon.  Is it possible to
> do this in any version of Couch?
>
> Thanks,
> Dan DeLeo
>