You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Duy Nguyen <du...@gmail.com> on 2009/11/04 17:26:19 UTC

mapping on non existing fields

Hi,

Im new to couchDB and im curious what couchDB behaves when I try to MAPPING
non-existing fields.

For example:

Document 1 has field1, field2, field3
Document 2 has field1, fied4, field5

function1 (doc) {
      emit (doc.field1, doc.field4);
}

function2 (doc) {
      emit (doc.field6, doc.field7);
}

What are the results from function 1 and function 2 ?

Thanks.

Re: mapping on non existing fields

Posted by Brian Candler <B....@pobox.com>.
On Wed, Nov 04, 2009 at 01:59:43PM -0500, Adam Kocoloski wrote:
>> Im new to couchDB and im curious what couchDB behaves when I try to  
>> MAPPING
>> non-existing fields.
>>
>> For example:
>>
>> Document 1 has field1, field2, field3
>> Document 2 has field1, fied4, field5
>>
>> function1 (doc) {
>>      emit (doc.field1, doc.field4);
>> }
>>
>> function2 (doc) {
>>      emit (doc.field6, doc.field7);
>> }
>>
>> What are the results from function 1 and function 2 ?
>>
>> Thanks.
>
> function2 will throw an exception in the view server.  It's not fatal,  
> but it's expensive, so not a good idea.  You should check for the  
> presence of the field first.

A convenient shorthand for this is:

function2(doc) {
  emit(doc.field6 || null, doc.field7 || null);
}

or to avoid emitting the key if field6 is not present,

function2(doc) {
  if (doc.field6) {
    emit(doc.field6, doc.field7 || null);
  }
}

Re: mapping on non existing fields

Posted by Adam Kocoloski <ko...@apache.org>.
On Nov 4, 2009, at 11:26 AM, Duy Nguyen wrote:

> Hi,
>
> Im new to couchDB and im curious what couchDB behaves when I try to  
> MAPPING
> non-existing fields.
>
> For example:
>
> Document 1 has field1, field2, field3
> Document 2 has field1, fied4, field5
>
> function1 (doc) {
>      emit (doc.field1, doc.field4);
> }
>
> function2 (doc) {
>      emit (doc.field6, doc.field7);
> }
>
> What are the results from function 1 and function 2 ?
>
> Thanks.

function2 will throw an exception in the view server.  It's not fatal,  
but it's expensive, so not a good idea.  You should check for the  
presence of the field first.  Best,

Adam