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

tri*_*rin 5 java mongodb

我尝试在互联网上搜索此错误,但没有任何帮助。我正在尝试使用aggregateJava 来使用 mongodb 中的函数。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);
Run Code Online (Sandbox Code Playgroud)

安装的 MongoDB 版本是:3.2.11

fel*_*lix 2

您在 $group 阶段中输入了两次“_id”。尝试更换这个

groupFields = new BasicDBObject("_id", 0); 
groupFields.put("count",new BasicDBObject("$sum",1)); 
groupFields.put("_id", "$RetailerZip"); 
Run Code Online (Sandbox Code Playgroud)

经过

groupFields = new BasicDBObject("_id", "$RetailerZip"); 
groupFields.put("count",new BasicDBObject("$sum",1)); 
Run Code Online (Sandbox Code Playgroud)