Search code examples
mongodbmapreducefield

How to perform a map reduce in MongoDB with a colon in the field name?


In MongoDB I have fields with a colon in the name. To address a field I would normally use:

var map = function() {
              emit(this._id, this.sth.field);
}

but with a colon inside the field name like:

var map = function() {
               emit(this._id, this.sth.fie:ld);
}

MongoDB returns JavaScript execution failed: SyntaxError: Unexpected token :

How can I solve this problem?


Solution

  • Property names in JavaScript can be quoted or unquoted.

    When the property name results in invalid JavaScript syntax, you need to switch to the quoted technique: this.sth['fie:ld'].

    var map = function() {
        emit(this._id, this.sth['fie:ld']);
    }