I am performing an aggregation operation using the java mongodb driver, and I have followed the example from the docs (inserted below). Accordingly, the _id field should be hidden. However, in my experience with my own code, as well as with the output of this example, the _id field _id not hidden even when the projection value is set to 0 (it works from the Mongo shell). Does anyone know if this is a bug in the mongodb java driver? Or am I doing something wrong?
// create our pipeline operations, first with the $match DBObject match = new BasicDBObject("$match", new BasicDBObject("type", "airfare") ); // build the $projection operation DBObject fields = new BasicDBObject("department", 1); fields.put("amount", 1); fields.put("_id", 0); DBObject project = new BasicDBObject("$project", fields ); // Now the $group operation DBObject groupFields = new BasicDBObject( "_id", "$department"); groupFields.put("average", new BasicDBObject( "$avg", "$amount")); DBObject group = new BasicDBObject("$group", groupFields); // run aggregation AggregationOutput output = collection.aggregate( match, project, group );
source share