trin
trin

Reputation: 75

Command failed with error 16410: 'FieldPath field names may not start with '$'.'

I have tried searching on internet about this error, but nothing helped. I am trying to use aggregate function in mongodb using Java. RetailerZip is the field I want to group my result.

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 version installed is: 3.2.11

Upvotes: 5

Views: 4711

Answers (1)

felix
felix

Reputation: 9285

you put "_id" twice in your $group stage. try to replace this

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

by

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

Upvotes: 2

Related Questions