couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Candler <B.Cand...@pobox.com>
Subject Re: mapping on non existing fields
Date Thu, 05 Nov 2009 07:46:16 GMT
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);
  }
}

Mime
View raw message