我想在iccube(http://www.iccube.com/support/documentation/user_guide/schemas_cubes/ds_mongodb.php#mapReduce)中创建一个mongodb/mapreduce数据源,下面的脚本在mongo shell中运行良好,它的格式应该如何才能被iccube接受,当我将相同的代码粘贴到iccube data source builder中时,我会收到以下错误消息:

MongoDB : invalid JSON (table:Test.mapReduce) : [var location_map = function() { if (this.companyId = "1234" && this.Parent_Location !== undefined && this.Parent_Location.value.length > 0 && this.Parent_Location.value[0].id !== undefined && this.Parent_Location.value[0].id !== null) { emit(this.Parent_Location.value[0].id, {Location_NameOfLocation: this.Location_NameOfLocation, LocationID: this._id, CountryName: "", companyId : 0}); } } var country_map = function() { if (this.companyId = "1234") { emit(this._id, { CountryName: this.CountryName, Location_NameOfLocation: "", LocationID: 0, companyId : this.companyId }); } } var r = function(key, values) { var result = {LocationID: 0, CountryName: "", Location_NameOfLocation: "", companyId : 0};    values.forEach(function(value) { if (result.LocationID === 0 && value.LocationID !== null ) { result.LocationID = value.LocationID; } if (result.companyId === 0 && value.companyId !== null ) { result.companyId = value.companyId; } if (result.CountryName === "" && value.CountryName !== null ) { result.CountryName = value.CountryName; } if (result.Location_NameOfLocation === "" && value.Location_NameOfLocation !== null ) { result.Location_NameOfLocation = value.Location_NameOfLocation; } }); return result; } db.runCommand( { mapReduce: Location, map: location_map, reduce: r, out: { replace: LocationsCountries }, query: {companyId : "1234"} } ) db.runCommand( { mapReduce: Countries, map: country_map, reduce: r, out: { reduce: LocationsCountries }, query: {companyId : "1234" } } )] ^

Mongo MapReduce脚本:
var location_map = function() {
  if (this.companyId = "1234" && this.Parent_Location !== undefined && this.Parent_Location.value.length > 0 && this.Parent_Location.value[0].id !== undefined && this.Parent_Location.value[0].id !== null) {
  emit(this.Parent_Location.value[0].id, {Location_NameOfLocation: this.Location_NameOfLocation, LocationID: this._id, CountryName: "", companyId : 0});
  }
}
var country_map = function() {
if (this.companyId = "1234") {
    emit(this._id, { CountryName: this.CountryName,  Location_NameOfLocation: "", LocationID: 0, companyId : this.companyId });
    }
}
var r = function(key, values) {
    var result = {LocationID: 0, CountryName: "", Location_NameOfLocation: "", companyId : 0};
    values.forEach(function(value) {
        if (result.LocationID === 0 &&  value.LocationID !== null  ) {   result.LocationID = value.LocationID;        }
        if (result.companyId === 0 &&  value.companyId !== null  ) {   result.companyId = value.companyId;        }
        if (result.CountryName === "" &&  value.CountryName !== null  ) {   result.CountryName = value.CountryName;        }
        if (result.Location_NameOfLocation === "" &&  value.Location_NameOfLocation !== null  ) {   result.Location_NameOfLocation = value.Location_NameOfLocation;        }
    });
    return result;
}
db.runCommand(
               {
                 mapReduce: "Location",
                 map: location_map,
                 reduce: r,
                 out: { replace: "LocationsCountries" },
                 query: {companyId : "1234"}
               }
             )

db.runCommand(
               {
                 mapReduce: "Countries",
                 map: country_map,
                 reduce: r,
                 out: { reduce: "LocationsCountries" },
                 query: {companyId : "1234" }
               }
             )

谢谢,
巴林特

最佳答案

您必须在字符串中定义函数(如果这些字符串中需要字符串,还需要javascript转义)。iccube文档(www)对此进行了描述。下面是一个将单引号字符串与双引号字符串组合的示例。

{
    "mapReduce": "Location",
    "map": 'function() {
        if (this.companyId = "1234" && this.Parent_Location !== undefined && this.Parent_Location.value.length > 0 && this.Parent_Location.value[0].id !== undefined && this.Parent_Location.value[0].id !== null) {
            emit(this.Parent_Location.value[0].id, {Location_NameOfLocation: this.Location_NameOfLocation, LocationID: this._id, CountryName: "", companyId : 0});
        }
    }',
    "reduce": 'function(key, values) {
            var result = {LocationID: 0, CountryName: "", Location_NameOfLocation: "", companyId : 0};
            values.forEach(function(value) {
                if (result.LocationID === 0 &&  value.LocationID !== null  ) {   result.LocationID = value.LocationID;        }
                if (result.companyId === 0 &&  value.companyId !== null  ) {   result.companyId = value.companyId;        }
                if (result.CountryName === "" &&  value.CountryName !== null  ) {   result.CountryName = value.CountryName;        }
                if (result.Location_NameOfLocation === "" &&  value.Location_NameOfLocation !== null  ) {   result.Location_NameOfLocation = value.Location_NameOfLocation;        }
            });
            return result;
        }',
    "out": {
        "replace": "LocationsCountries"
    },
    "query": {
        "companyId" : "1234"
    }
}

09-25 17:15