只是「翻譯」到這個org.bson.Document結構(類似地圖),並調用適當的措施(這是不是一個查詢,在這種情況下,它是一個集合):
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2003-01-01");
Document group = new Document();
group.put("_id", new Document("$year", "$Publication_Date"));
group.put("total", new Document("$sum", 1));
AggregateIterable<Document> aggregate = collection.aggregate(Arrays.asList(
new Document("$match", new Document("Publication_Date",
new Document("$gte", date))),
new Document("$group", group)
));
或使用com.mongodb.client.model包靜態方法(更簡潔):
AggregateIterable<Document> aggregate = collection.aggregate(Arrays.asList(
Aggregates.match(Filters.gte("Publication_Date", date)),
Aggregates.group(
new Document("$year", "$Publication_Date"),
Accumulators.sum("total", 1))));
請考慮包括從您的收藏樣本文件,你已經嘗試到目前爲止的代碼 – Veeram