2016-11-27 76 views
3

我試過在網上搜索這個錯誤,但沒有任何幫助。我正嘗試在使用Java的mongodb中使用aggregate函數。 RetailerZip是我想將我的結果分組的字段。安裝

groupFields = new BasicDBObject("_id", 0); 
groupFields.put("count",new BasicDBObject("$sum",1)); 
groupFields.put("_id", "$RetailerZip"); 
group = new BasicDBObject("$group", groupFields); 
sort = new BasicDBObject(); 
projectFields = new BasicDBObject("_id", 0); 

projectFields.put("value", "$_id"); 
projectFields.put("ReviewValue","$count"); 
project = new BasicDBObject("$project", projectFields); 
sort.put("ReviewValue",-1); 
orderby=new BasicDBObject("$sort",sort); 
limit=new BasicDBObject("$limit",5); 

List<DBObject> pipeline = Arrays.asList(group, project, orderby, limit); //error occurs on this line. 
AggregationOutput output = mongo.myReviews.aggregate(pipeline); 

MongoDB的版本是:3.2.11

+1

您可以加入你所得到的錯誤的詳細信息?還有什麼'組','項目'等是對象? – nullpointer

+0

什麼是'RetailerZip'沒有在問題中提到。問題肯定需要改進。 – nullpointer

回答

1

你把 「_id」 兩次在$小組賽階段。 試圖取代這一

groupFields = new BasicDBObject("_id", 0); 
groupFields.put("count",new BasicDBObject("$sum",1)); 
groupFields.put("_id", "$RetailerZip"); 

通過

groupFields = new BasicDBObject("_id", "$RetailerZip"); 
groupFields.put("count",new BasicDBObject("$sum",1)); 
+0

謝謝!有效。 – trin

相關問題