Hi Nifras,
What MongoDB version and MongoDB Java Driver version are you using ?
In mongo-java-driver v3.0, the aggregate method accepts as its argument an array of stages. Also worth noting that if you are using mongo-java-driver v3.0 and writing a new application, try to use Document instead of DBObject. For example:
import static java.util.Arrays.asList;
MongoDatabase db = mongoClient.getDatabase("shop");
Document id = new Document("customer_id", "$customer_id").append("time_id", "$time_id");
Document products = new Document("$push", "$product_id");
Document group1 = new Document("_id", id).append("products", products );
List<Document> pipeline = asList(new Document("$group", group1));
AggregateIterable<Document> iterable = db.getCollection("transactions").aggregate(pipeline);
iterable.forEach(new Block<Document>() {
@Override
public void apply(final Document document) {
System.out.println(document.toJson());
}
});
mongoClient.close();
See Data Aggregation with Java Driver for more info.
The above snippet was tested in:
Regards,
Wan.