For the Ticket: UserReport using Java Driver, I prepared the aggregation pipeline via the Compass tool and I am able to get the top 20 comments. I exported the code into Java and trying to iterate over the result but encountering following exceptions.
Code :
public List<Critic> mostActiveCommenters() {
List<Critic> mostActive = new ArrayList<>();
List<Bson> pipeline = Arrays.asList(
new Document("$group",
new Document("_id", "$email")
.append("comments", new Document("$sum", 1L))),
new Document("$sort", new Document("comments", -1L)),
new Document("$limit", 20L)
);
MongoCursor cursor = commentCollection
.withReadConcern(ReadConcern.MAJORITY)
.aggregate(pipeline)
.iterator();
while(cursor.hasNext()){
Document commentDoc = (Document) cursor.next();
Critic critic = new Critic(commentDoc.getString("email"),
commentDoc.getInteger("comments"));
mostActive.add(critic);
}
return mostActive;
}
I understand that the resulting output of the group stage does not contain a _id of Object type and therefore Codec is failing to create an instance of Comment POJO.
Could anyone suggest some pointers on how to resolve this or any alternatives that I can explore.
Thanks.
Encountering the exception on the iterator :
org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'Comment' failed with the following exception:
Failed to decode 'Comment'. Decoding '_id' errored with: readObjectId can only be called when CurrentBSONType is OBJECT_ID, not when CurrentBSONType is STRING.
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:40)
at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
at org.bson.codecs.configuration.LazyCodec.decode(LazyCodec.java:47)
at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101)
at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:63)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
at com.mongodb.internal.connection.ReplyMessage.<init>(ReplyMessage.java:51)
at com.mongodb.internal.connection.InternalStreamConnection.getCommandResult(InternalStreamConnection.java:409)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:305)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99)
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:444)
at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72)
at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:200)
at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269)
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131)
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:123)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:242)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:233)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:136)
at com.mongodb.operation.AggregateOperationImpl$1.call(AggregateOperationImpl.java:193)
at com.mongodb.operation.AggregateOperationImpl$1.call(AggregateOperationImpl.java:189)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:462)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:406)
at com.mongodb.operation.AggregateOperationImpl.execute(AggregateOperationImpl.java:189)
at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:295)
at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:41)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:179)
at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132)
at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:86)