javascript - How to use scope variables as property names in a Mongo Map/Reduce emit -


there question (and answer) deals general case. having difficulty using scope variable field key (as opposed field value)

in example below fully_caps fields scope variables. in case of service , identifier emit correctly uses value of scope variable passed m/r.

however when try use value of scope variable key in emitted document, document created scope variable name (as opposed it's value).

return emit({     service: service,     date: _this.value.date,     identifier: _this.value[identifier]   }, {     errors: {       count: 1,       type_breakdown: {         singles_only: {           count: 1         }       }     }   }); 

is there way around problem?

when using shortcut syntax creating objects in javascript, left hand side/property name interpreted literal value, regardless of quotes.

for example:

var d={ name: "aaron" } 

is equivalent to:

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

as there 2 ways set property value:

  1. obj.propertyname=value
  2. obj["propertname"]=value

you have construct object using second syntax, @ least in part.

var errors={       count: 1,       type_breakdown: { }       }     }; var countobj={ count:1 }; errors.type_breakdown[singles_only]=countobj;  // pass results emit call 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -