mongodb java driver - 无法在map函数中使用split

时间:2015-06-21 11:49:11

标签: java mongodb

我的表格中包含

格式的文件
{
  "_id" : ObjectId("55867f3bfe2b0bea9c634ded"),
  "Best Site CR GU Party ID" : 14783,
  "Best Site CR GU Party Name" : "AUTOMOBILE INSURANCE COMPANY",
  "Product ID" : "C5500",
  "Product Family" : "C5000",
  "Product Description" : "5500 Chassis",
  "Best Site CR Party ID" : 45714558,
  "Best Site CR Party Name" : "INSURANCE COMPANY",
  "LDoS" : "4/27/2008",
  "LDoS FY" : 2008,
  "List Price $" : 3495,
  "Collector View" : "No"
}

我正在使用mongodb java driver 3.0.2。目的是汇总集合中每年每个月的价格和文件数量。我为map创建了以下函数,它适用于MongoVUE并且使用java驱动程序失败:

function Map() {
    var currentYr = new Date().getFullYear();
    var ldosMonth = this['LDoS'].split(/[//-]/)[0].replace(/^0+/, '');
    var key = {partyId: this['Best Site CR GU Party ID'], year: this['LDoS FY'], month: parseInt(ldosMonth)};

    emit(key, {price: this['List Price $'], count: 1});
}

Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 16722: 'exception: TypeError: Object 42582 has no method 'split'
    at Map (_funcs1:1:99) near '['LDoS']).split(/[//-]/)[0].replace(/^0+/' ' on server localhost:27017. The full response is { "errmsg" : "exception: TypeError: Object 42582 has no method 'split'\n    at Map (_funcs1:1:99) near '['LDoS']).split(/[//-]/)[0].replace(/^0+/' ", "code" : 16722, "ok" : 0.0 }
    at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:77)

我该如何解决这个问题?为了完整起见,这里是reduce函数:

function Reduce(key, values) {
    var reduced = {price:0, count:0};

    values.forEach(function(val) {
        reduced.price += val.price;
        reduced.count += val.count; 
    });

    return reduced;
}

 String map = "function Map() {" +
            "var currentYr = new Date().getFullYear();" +
            "var ldosMonth = (this['LDoS']).split(/[//-]/)[0].replace(/^0+/, ''); " + // trim leading zero
            "var key = {partyId: this['Best Site CR GU Party ID'], year: this['LDoS FY'], month: parseInt(ldosMonth)};" +
            "var objToEmit = {price: this['List Price $'], count: 1};" +
            "emit(key, objToEmit);" +               
        "}";

        String reduce = "function Reduce(key, values) {" +
            "var reduced = {price:0, count:0};" +

            "values.forEach(function(val) {" +
                "reduced.price += val.price;" +
                "reduced.count += val.count;" + 
            "});" +

            "return reduced;" + 
        "}";

        MapReduceIterable<Document> output = dbCollection.mapReduce(map, reduce);

1 个答案:

答案 0 :(得分:0)

我通过重构mapReduce调用

来解决这个问题
import collections
d = collections.defaultdict(dict)
for i in kinetic_parameters:
    d[i[1]][i[2]]=i[3]