3

我想在 icCube 中创建一个 MongoDB/mapReduce 数据源(http://www.iccube.com/support/documentation/user_guide/schemas_cubes/ds_mongodb.php#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" }
               }
             )       

谢谢, 巴林特

4

1 回答 1

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"
    }
}
于 2015-05-18T12:25:13.390 回答