Skip to content Skip to sidebar Skip to footer

How To Use Scope Variables As Property Names In A Mongo Map/reduce Emit

There is a question (and answer) that deals with the general case. I am having difficulty using a scope variable as a field key (as opposed to the field value) In the example below

Solution 1:

When using the shortcut syntax for creating objects in JavaScript, the left hand side/property name is always interpreted as a literal value, regardless of quotes.

For example:

var d={ name: "Aaron" }

Is equivalent to:

var d={ "name" : "Aaron" }

As there are two ways to set a property value:

  1. obj.propertyName=value
  2. obj["propertName"]=value

You have to construct your object using the second syntax, at least in part.

var errors={
      count: 1,
      type_breakdown: { }
      }
    };
var countObj={ count:1 };
errors.type_breakdown[SINGLES_ONLY]=countObj;

// pass results to emit call

Post a Comment for "How To Use Scope Variables As Property Names In A Mongo Map/reduce Emit"