2015-06-21 70 views
1

使用分裂我和文檔表中的MongoDB的Java驅動程序 - 無法在地圖功能

{ 
    "_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驅動程序3.0.2。目的是彙總收集中每年每月的價格和文件數量。我創建在地圖下面的函數,它適用於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) 

我怎樣才能解決這個問題?爲了完整起見,這裏的減少功能:

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); 

回答

0

我得到這個工作通過重構MapReduce的呼叫

MapReduceIterable<Document> output = dbCollection.mapReduce(map, reduce) 
       .finalizeFunction("function Finalize(key, reduced) {return reduced;}") 
       .filter(Filters.and(new BasicDBObject("Best Site CR GU Party ID", guCrPartyId), Filters.or(new BasicDbObject("LDoS FY", 2015), new BasicDbObject("LDoS FY", 2016),new BasicDbObject("LDoS FY", 2017)) 
       .action(MapReduceAction.REPLACE);