0

Here is part of a stack trace I am getting when I call the aggregate method on mongodb's DBCollection object:

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

I am confused as to why I am getting this error and how to resolve it.

The first thing that came to mind is perhaps I have an old version of the driver in my path which does not support this method?

I removed all old versions of the driver and used only version 2.13.0.

I validated that my code was actually calling that version of the driver like so:

Code:

  logger.debug("major version: " + Mongo.MAJOR_VERSION);
  logger.debug("minor version: " + Mongo.MINOR_VERSION);

Output:

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

This version of the driver is supposed to support this method: http://api.mongodb.org/java/2.13/com/mongodb/DBCollection.html#aggregate-java.util.List-

So, I'm guessing that the driver is throwing an unhelpful exception and something else is going on here.

I've tested the query that I am running and it works well from the mongo shell. It looks like this:

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}}  
])

It returns results like this:

  { "_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 }

Here is the java code that I am using to execute the same query via the java driver:

  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);

I've validated that the query in the java code matches the query that I run in the mongo shell with the debugging statements:

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}}|

So, I'm not sure why this exception is being raised or how to fix it.

Alex Ryan
  • 3,719
  • 5
  • 25
  • 41

2 Answers2

0

Looks like this might be a bug in the driver. Or possibly I am using it incorrectly?

When I used the older deprecated version of the aggregate method, it worked.

  AggregationOutput output = this.collection.aggregate(match, group, sort);
Alex Ryan
  • 3,719
  • 5
  • 25
  • 41
0

Try the older version of mongo-java-driver jar i.e. 2.11.4 to resolve the problem. Here's a link to download the jar.