在此处找到的示例(http://mongodb.github.io/mongo-java-driver/3.2/builders/aggregation/)中,聚合分组基于"$customerId"
:
collection.aggregate(Arrays.asList(match(eq("author", "Dave")),
group("$customerId", sum("totalQuantity", "$quantity"),
avg("averageQuantity", "$quantity"))
out("authors")));
"$systemId"
? 在 mongo-shell 中,您可以执行以下操作:
db.coll.aggregate(
[
{
$group : {
_id : {"customerId":"$customerId", "systemId":"$systemId"},
//etc
}
}
]
)
在 Java 中,您可以构建一个查询:
DBObject groupFields = new BasicDBObject();
DBObject groupIdFields = new BasicDBObject();
groupIdFields.put("customerId", "$customerID");
groupIdFields.put("systemId", "$systemId");
groupFields.put( "_id", groupIdFields );
//...
//... here some more aggregate criteria
//...
final AggregationOutput output = coll.aggregate(group);
现在,这似乎是一个非常多的代码,只是为了获得多个分组标准。如何使用 mongo 3 java驱动程序中的聚合管道?