在icCube中创建Mongo / mapReduce数据源

时间:2015-05-18 08:53:22

标签: mongodb iccube

我想在icCube(http://www.iccube.com/support/documentation/user_guide/schemas_cubes/ds_mongodb.php#mapReduce)中创建一个MongoDB / mapReduce数据源,下面的脚本在Mongo shell中运行良好,当我粘贴相同的代码时,icCube应该如何格式化它进入icCube数据源构建器,我收到此错误消息:

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" }
               }
             )       

谢谢, 巴林特

1 个答案:

答案 0 :(得分:2)

您必须在字符串中定义函数(如果在这些字符串中需要字符串,则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"
    }
}