Mongo / Java Map-Reduce函数 - MongoCommandException

时间:2016-10-18 13:13:40

标签: java mongodb function dictionary

我收到以下错误: com.mongodb.MongoCommandException:命令失败,错误139:' SyntaxError:missing;在陈述@:1:60之前 '在服务器localhost:27017。完整的回答是{" ok" :0.0," errmsg" :" SyntaxError:missing;在陈述@:1:60 \ n","代码" :139

似乎我错过了一个;但似乎无法看到哪里。该方法不言自明,我倾向于通过mongo创建map / reduce。任何帮助表示赞赏。

public void mapFunction()
           {
               Mongo mongo = null;
               try{
               mongo = new Mongo("localhost", 27017);
               DB db = mongo.getDB("amarokforumdb");

               DBCollection comments = db.getCollection("comments");  
               String map = "function() { "+
                     "var category; " + 
                         "var numOfDocuments = "+db+".comments.count();" +

                         "for(i = 0; i < numOfDocuments; i++){ " +
                     "if (numOfDocuments <= 100) {"+ 
                     "category = 'New Comments'; }" +
                     "else if(numOfDocuments > 100){" +
                     "category = 'Old Comments'; }}"+ 
                     "emit(category,1);};";

               String reduce = "function(key, values) { " +
                                    "var sum = 0; " +
                                    "values.forEach(function(doc) { " +
                                    "sum += 1; "+
                                    "}); " +
                                    "return {comments: sum};} ";


                MapReduceCommand cmd = new MapReduceCommand(comments, map, reduce,
            null, MapReduceCommand.OutputType.INLINE, null);

            MapReduceOutput out = comments.mapReduce(cmd);



               for (DBObject o : out.results()) {
            System.out.println(o.toString());
               }}catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
          }

           }

1 个答案:

答案 0 :(得分:1)

"+db+".comments.count();替换为整数以进行测试,我认为您+db+部分出了问题