命令失败,错误16410:' FieldPath字段名称可能不以' $'开头。'。'

时间:2016-11-27 05:57:23

标签: java mongodb

我曾尝试在互联网上搜索此错误,但没有任何帮助。我正在尝试使用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 个答案:

答案 0 :(得分:1)

你在$ group阶段两次放“_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));