这是我在 mongodb 的 DBCollection 对象上调用聚合方法时得到的堆栈跟踪的一部分:
java.lang.NoSuchMethodError: com.mongodb.DBCollection.aggregate(Ljava/util/List;)Lcom/mongodb/AggregationOutput;
at com.alpine.dao.AppEventDAO.getActiveDeviceIds(AppEventDAO.java:975)
at sonomaLoggerTest.AppEventDAO_getActiveDeviceIds.test(AppEventDAO
我对为什么会收到此错误以及如何解决它感到困惑。
首先想到的可能是我的路径中有一个旧版本的驱动程序不支持这种方法?
我删除了所有旧版本的驱动程序,只使用了 2.13.0 版本。
我验证了我的代码实际上调用了该版本的驱动程序,如下所示:
代码:
logger.debug("major version: " + Mongo.MAJOR_VERSION);
logger.debug("minor version: " + Mongo.MINOR_VERSION);
输出:
2015-03-25 15:50:58,242 DEBUG: AppEventDAO.java:972: major version: 2
2015-03-25 15:50:58,243 DEBUG: AppEventDAO.java:973: minor version: 13
这个版本的驱动应该支持这个方法: http ://api.mongodb.org/java/2.13/com/mongodb/DBCollection.html#aggregate-java.util.List-
所以,我猜测驱动程序抛出了一个无用的异常,并且这里发生了其他事情。
我已经测试了我正在运行的查询,它在 mongo shell 中运行良好。它看起来像这样:
db.event.aggregate([
{ "$match" : { "$and" : [ { "time" : { "$gte" : 1426834800}} , { "time" : { "$lt" : 1427353200}} , { "type" : "SONG_PLAY_STARTED"}]}},
{ "$group" : { "_id" : "$device.id" , "count" : { "$sum" : 1}}},
{ "$sort" : { "count" : -1}}
])
它返回如下结果:
{ "_id" : "D44A4299A9594FCFBE05CE8FB93F6826", "count" : 79 }
{ "_id" : "8E653C9D8E87452199DAA2BB9D908AEA", "count" : 40 }
{ "_id" : "C74CE4D520034020B4C353235F1AFBC0", "count" : 27 }
{ "_id" : "E245CEFACBC84604AF904BD8DE4D045D", "count" : 27 }
{ "_id" : "2D8C085876AB4FE39B18DC9382975238", "count" : 25 }
{ "_id" : "43466F0F8BD542EFA2CECA3B2997C997", "count" : 23 }
{ "_id" : "389A389EE003455994941429973A81FE", "count" : 20 }
{ "_id" : "9A76A14E7308440D8F1108052787A409", "count" : 12 }
{ "_id" : "4B8126E9587D475C8934C20FCBF4D7DB", "count" : 11 }
{ "_id" : "393FFE19397D4586AD6950C2264A087A", "count" : 10 }
这是我用来通过 java 驱动程序执行相同查询的 java 代码:
DBObject clause1 = new BasicDBObject("time", new BasicDBObject("$gte", startTime));
DBObject clause2 = new BasicDBObject("time", new BasicDBObject("$lt", endTime));
DBObject clause3 = new BasicDBObject("type", "SONG_PLAY_STARTED");
BasicDBList andClauses = new BasicDBList();
andClauses.add(clause1);
andClauses.add(clause2);
if (excludeNoSongsPlayed) {
andClauses.add(clause3);
}
BasicDBObject and = new BasicDBObject("$and", andClauses);
DBObject match = new BasicDBObject("$match", and);
DBObject groupFields = new BasicDBObject( "_id", "$device.id");
groupFields.put("count", new BasicDBObject( "$sum", 1));
DBObject group = new BasicDBObject("$group", groupFields);
// Finally the $sort operation
DBObject sort = new BasicDBObject("$sort", new BasicDBObject("count", -1));
logger.debug("match=|" + match + "|");
logger.debug("group=|" + group + "|");
logger.debug("sort=|" + sort + "|");
List<DBObject> pipeline = Arrays.asList(match, group, sort);
logger.debug("major version: " + Mongo.MAJOR_VERSION);
logger.debug("minor version: " + Mongo.MINOR_VERSION);
AggregationOutput output = this.collection.aggregate(pipeline);
我已经验证了 java 代码中的查询与我在 mongo shell 中运行的带有调试语句的查询相匹配:
2015-03-25 15:50:58,238 DEBUG: AppEventDAO.java:958: match=|{ "$match" : { "$and" : [ { "time" : { "$gte" : 1426834800}} , { "time" : { "$lt" : 1427353200}} , { "type" : "SONG_PLAY_STARTED"}]}}|
2015-03-25 15:50:58,239 DEBUG: AppEventDAO.java:959: group=|{ "$group" : { "_id" : "$device.id" , "count" : { "$sum" : 1}}}|
2015-03-25 15:50:58,242 DEBUG: AppEventDAO.java:960: sort=|{ "$sort" : { "count" : -1}}|
因此,我不确定为什么会引发此异常或如何修复它。