我正在使用MongoDB聚合框架查询一个文档,结果如下:
{
"result" : [
{
"_id" : "luke",
"times" : 8
},
{
"_id" : "albert",
"times" : 4
},
{
"_id" : "matt",
"times" : 4
}
],
"ok" : 1
}
从上面的结果可以看出,查询在 mongoDB shell 中工作,但是我在使用 Jongo 获取结果时遇到了问题:
Aggregationoutput =
gamesCollection.aggregate(
"{ ... }"
).as(Aggregation.class);
output.results().iterator().hasNext();
主要问题似乎是 Jongo 不允许我使用AggregationOutput
?他想要Aggregation
...但找不到任何关于如何使用它的示例
编辑:
我有点沮丧,我不能让 Jongo 使用聚合。DBObjects
我必须按照 MongoDB Java 驱动程序中指定的方式编写查询,但代码看起来真的很难看..
EDIT2: 只是为了完成信息,这是我与 Jongo 一起使用的原始聚合,它无法解组为 ResultObject
List<ResultObject> output =
gamesCollection.aggregate(
"{ $match: { 'playersList.playerid': 'bob' }},"
+"{ $unwind: '$playersList' },"
+"{ $match: { 'playersList.playerid': { $ne: 'bob' } } },"
+"{ $group: { _id: '$playersList.playerid', times: { $sum : 1} } },"
+"{ $sort: { times: -1 } }"
).as(ResultObject.class);
class ResultObject{
String _id;
int times;
}
}