Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2471

aggregation on view doesn't work

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.5.0
    • Affects Version/s: 3.4.2
    • Component/s: Query Operations
    • None
    • Environment:
      ubuntu 16.04, single mongod instance, no shard, no replica set. Used along with spring-data-mongodb 1.10.1

      Aggregation on view doesn't work when run with java driver, but works fine in shell.

      sample code to reproduce the issue:

      
      MongoClient client = new MongoClient("localhost", 27017);
      SimpleMongoDbFactory factory = new SimpleMongoDbFactory(client, "databaseName");
      MongoTemplate mongoTemplate = new MongoTemplate(factory);
      List<DBObject> pipeline = new ArrayList<>();
      BasicDBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1));
      pipeline.add(sort);
      
      AggregationOutput output = mongoTemplate.getCollection("viewCollection").aggregate(pipeline);
      for (DBObject obj : output.results()) {
         String id = (String) obj.get("_id");
         System.out.println("ID IS " + id);
      }
      

      Error shows up with both Cursor and AggregationOutput

      stack trace:

      Exception in thread "main" org.bson.BsonInvalidOperationException: Document does not contain key result
      at org.bson.BsonDocument.throwIfKeyAbsent(BsonDocument.java:844)
      at org.bson.BsonDocument.getArray(BsonDocument.java:147)
      at com.mongodb.operation.BsonDocumentWrapperHelper.toList(BsonDocumentWrapperHelper.java:28)
      at com.mongodb.operation.AggregateOperation.createQueryResult(AggregateOperation.java:356)
      at com.mongodb.operation.AggregateOperation.access$700(AggregateOperation.java:67)
      at com.mongodb.operation.AggregateOperation$3.apply(AggregateOperation.java:367)
      at com.mongodb.operation.AggregateOperation$3.apply(AggregateOperation.java:364)
      at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:216)
      at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:207)
      at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:113)
      at com.mongodb.operation.AggregateOperation$1.call(AggregateOperation.java:257)
      at com.mongodb.operation.AggregateOperation$1.call(AggregateOperation.java:253)
      at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:431)
      at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:404)
      at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:253)
      at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:67)
      at com.mongodb.Mongo.execute(Mongo.java:836)
      at com.mongodb.Mongo$2.execute(Mongo.java:823)
      at com.mongodb.DBCollection.aggregate(DBCollection.java:1455)
      at com.mongodb.DBCollection.aggregate(DBCollection.java:1380)
      at com.mongodb.DBCollection.aggregate(DBCollection.java:1366)
      at com.mycompany.test.Main.main(Main.java:200)

        1. JAVA2471.java
          5 kB
          Ross Lawley

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            felix2626 adrien petel
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: