[JAVA-2954] Aggregation is not working with Java driver able to get the result with shell Created: 30/Aug/18  Updated: 27/Oct/23  Resolved: 31/Aug/18

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Sowjanya Kommi Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

        MongoDatabase database = client.getDatabase("EDelivery");
        MongoCollection<Document> collection = database.getCollection("PackageGroup");
        Block<Document> processBlock = new Block<Document>() {
            @Override
            public void apply(final Document document) {
                System.out.println(document);
            }
        };
        List<? extends Bson> pipeline = Arrays.asList(
                new Document().
                        append("$match", new Document().append("GroupId", "234524")),
                new Document().
                        append("$unwind", new Document().append("path", "$Packages")),
                new Document().
                        append("$match", new Document().append("Packages._id", "e3adda7b-68e6-4b5c-81d1-5af6cf4a8571")),
                new Document().
                        append("$project", new Document().append("SecurityToken", "$Packages.SecurityToken")));
 
        collection.aggregate(pipeline)
                .allowDiskUse(false)
                .forEach(processBlock);
    }

I'm able to execute Aggregation through Mongo shell however through Java driver I'm not able to 

Here is the code and error using 3.8.1 version of java driver.

 

Error:

 
 
Aug 30, 2018 4:29:30 PM com.mongodb.diagnostics.logging.JULLogger logAug 30, 2018 4:29:30 PM com.mongodb.diagnostics.logging.JULLogger logINFO: Opened connection [connectionId\\{localValue:2, serverValue:1012}] to ef1vmongodb3001.dco.elmae:27019Aug 30, 2018 4:29:30 PM com.mongodb.diagnostics.logging.JULLogger logINFO: Closed connection [connectionId\\{localValue:2, serverValue:1012}] to ef1vmongodb3001.dco.elmae:27019 because the pool has been closed.com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on EDelivery to execute command { aggregate: "PackageGroup", pipeline: [ { $match:
 
{ GroupId: "234524" }
 
}, { $unwind:
 
{ path: "$Packages" }
 
}, { $match:
 
{ Packages._id: "e3adda7b-68e6-4b5c-81d1-5af6cf4a8571" }
 
}, { $project:
 
{ SecurityToken: "$Packages.SecurityToken" }
 
} ], cursor: {}, allowDiskUse: false }' on server ef1vmongodb3001.dco.elmae:27019. The full response is { "ok" : 0.0, "errmsg" : "not authorized on EDelivery to execute command { aggregate: \"PackageGroup\", pipeline: [ { $match:
 
{ GroupId: \"234524" }
 
}, { $unwind:
 
{ path: \"$Packages\" }
 
}, { $match:
 
{ Packages._id: \"e3adda7b-68e6-4b5c-81d1-5af6cf4a8571\" }
 
}, { $project:
 
{ SecurityToken: \"$Packages.SecurityToken\" }
 
} ], cursor: {}, allowDiskUse: false }", "code" : 13 } at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:179) at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:293) 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:457) at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:401) at com.mongodb.operation.AggregateOperationImpl.execute(AggregateOperationImpl.java:189) at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:294) 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) at com.mongodb.client.internal.MongoIterableImpl.forEach(MongoIterableImpl.java:110) at net.codejava.mongodb.JavaMongoDBConnection.main(JavaMongoDBConnection.java:107)



 Comments   
Comment by Sowjanya Kommi [ 31/Aug/18 ]

@jeff yemi

 

The smae query I was able to execute with shell

 

Comment by Jeffrey Yemin [ 31/Aug/18 ]

The error Command failed with error 13: 'not authorized on EDelivery to execute command indicates that whatever user your Java application is authenticating as does not have authorization to query the EDelivery database.

This is a configuration error, not a bug in the driver.

Generated at Thu Feb 08 08:58:27 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.