[CSHARP-2255] Unable to execute any Run Commands using C# Driver V2.5 Created: 27/Apr/18  Updated: 28/Oct/23  Resolved: 27/Apr/18

Status: Closed
Project: C# Driver
Component/s: Command Operations
Affects Version/s: 2.5
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Clifton Sothoron Assignee: Peter Garafano (Inactive)
Resolution: Fixed Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10, Framework 4.5.2, Visual Basic, MongoDB 3.4 database



 Description   

I'm unable to successfully execute any run commands using the new C Sharp driver. We previously used the C Sharp driver V1.1. Similar commands worked properly in that environment. In addition I'm also using the MongoDB Java driver V3.6.3. The exact same commands work properly via RunCommand.

Here is the simplified VB source I'm using.
Dim _oClient As MongoClient = Nothing
Dim _oDatabase As MongoDatabase = Nothing
Dim _oCommandResult As CommandResult = Nothing
_oClient = New MongoClient("connstring")
_oDatabase = _oClient.GetServer().GetDatabase(New MongoUrl("connstring").DatabaseName)
Dim sCommand As String = "{aggregate:""restaurants"", pipeline:[{$group:{_id:""$borough"",borough:{$sum:1}}}]}"
_oCommandResult = _oDatabase.RunCommand(sCommand)

Here is the stacktrace that results. What is wrong? I get "no such command" messages no matter what syntax I try.

Thanks in advance,
Clifton Sothoron of Logi Analytics

Command {aggregate:"restaurants", pipeline:[{$group:{_id:"$borough",borough:{$sum:1}}}]} failed: no such command: '{aggregate:"restaurants", pipeline:[{$group:{_id:"$borough",borough:{$sum:1}}}]}', bad cmd: '{ {aggregate:"restaurants", pipeline:[{$group:{_id:"$borough",borough:{$sum:1}}}]}: 1 }'.
Source:
MongoDB.Driver.Core
Stack Trace:
at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver.Core\Core\WireProtocol\CommandWireProtocol.cs:line 189

at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.Execute(IConnection connection, CancellationToken cancellationToken) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver.Core\Core\WireProtocol\CommandWireProtocol.cs:line 154

at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocol[TResult](IWireProtocol`1 protocol, CancellationToken cancellationToken) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver.Core\Core\Servers\Server.cs:line 728

at MongoDB.Driver.Core.Servers.Server.ServerChannel.Command[TResult](ICoreSession session, ReadPreference readPreference, DatabaseNamespace databaseNamespace, BsonDocument command, IElementNameValidator commandValidator, BsonDocument additionalOptions, Func`1 responseHandling, Boolean slaveOk, IBsonSerializer`1 resultSerializer, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver.Core\Core\Servers\Server.cs:line 362

at MongoDB.Driver.Core.Operations.CommandOperationBase`1.ExecuteProtocol(IChannelHandle channel, ICoreSessionHandle session, ReadPreference readPreference, CancellationToken cancellationToken) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver.Core\Core\Operations\CommandOperationBase.cs:line 152

at MongoDB.Driver.Core.Operations.CommandOperationBase`1.ExecuteProtocol(IChannelSource channelSource, ICoreSessionHandle session, ReadPreference readPreference, CancellationToken cancellationToken) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver.Core\Core\Operations\CommandOperationBase.cs:line 182

at MongoDB.Driver.Core.Operations.WriteCommandOperation`1.Execute(IWriteBinding binding, CancellationToken cancellationToken) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver.Core\Core\Operations\WriteCommandOperation.cs:line 57

at MongoDB.Driver.OperationExecutor.ExecuteWriteOperation[TResult](IWriteBinding binding, IWriteOperation`1 operation, CancellationToken cancellationToken) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver\OperationExecutor.cs:line 44

at MongoDB.Driver.MongoDatabase.ExecuteWriteOperation[TResult](IClientSessionHandle session, IWriteOperation`1 operation) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver.Legacy\MongoDatabase.cs:line 1052

at MongoDB.Driver.MongoDatabase.RunCommandAs[TCommandResult](IClientSessionHandle session, IMongoCommand command, IBsonSerializer`1 resultSerializer, ReadPreference readPreference) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver.Legacy\MongoDatabase.cs:line 1084

at MongoDB.Driver.MongoDatabase.RunCommandAs[TCommandResult](IClientSessionHandle session, IMongoCommand command, ReadPreference readPreference) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver.Legacy\MongoDatabase.cs:line 923

at MongoDB.Driver.MongoDatabase.<>c_DisplayClass71_0`1.b_0(IClientSessionHandle session) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver.Legacy\MongoDatabase.cs:line 913

at MongoDB.Driver.MongoDatabase.UsingImplicitSession[TResult](Func`2 func) in C:\work\rstam\mongo-csharp-driver-v2.5.0\src\MongoDB.Driver.Legacy\MongoDatabase.cs:line 1110

at rdServer.MongoDBReader.OpenConnection() in C:\ReportDev\VB\rdServer\DataEngine\DataReaders\MongoDBReader.vb:line 853



 Comments   
Comment by Clifton Sothoron [ 30/Apr/18 ]

Pete,

Thank you for replying.  I was able to get a version of what you proposed working.

Dim oCmdDoc As New CommandDocument(MongoDB.Bson.Serialization.BsonSerializer.Deserialize(Of BsonDocument)(sCommand))

_oCommandResult = _oDatabase.RunCommand(oCmdDoc)

Best,

           Clifton Sothoron

Comment by Peter Garafano (Inactive) [ 27/Apr/18 ]

Hi Clifton,

The overload of the RunCommand method you are using there accepts a string of the command name. Since it assumes this is the command name, It does not parse the JSON. This results in the driver attempting to run a command that looks something like

{ "{aggregate:""restaurants"", pipeline:[{$group:{_id:""$borough"",borough:{$sum:1}}}]}": 1}

To get the aggregate command to run as you intend, you need to parse the JSON into a CommandDocument.

Dim client As MongoClient = New MongoClient
Dim db As IMongoDatabase = client.GetDatabase("test")
Dim commandString As String = "{aggregate:""restaurants"", pipeline:[{$group:{_id:""$borough"",borough:{$sum:1}}}]}"
Dim cmdDocument = CommandDocument.Parse(commandString)
Dim cmdResult As BsonDocument = db.RunCommand(Of BsonDocument)(cmdDocument)
Console.WriteLine(cmdResult)

In general, parsing JSON commands like this is inefficient and, depending on the source of the string, can pose a significant security risk. The driver provides a number of methods for building aggregations that aren't susceptible to these issues. For example, the same command can be built with

Dim commandDocument = New CommandDocument("aggregate","restaurants").Add("pipeline", New BsonArray().Add(New BsonDocument("$group",New BsonDocument("_id","$borough").Add("borough", New BsonDocument("$sum",1)))))

Or using the stronger typed method of

Dim col As IMongoCollection(Of BsonDocument) = db.GetCollection(Of BsonDocument)("test")
Dim agg = col.Aggregate().Group(New BsonDocument("_id","$borough").Add("borough", New BsonDocument("$sum",1)))
Dim res = agg.ToList()
Console.WriteLine(res)

Please keep in mind, the driver will provide even stronger typing if you want/need it. Additionally, I don't recommend calling ToList() on a result set of unknown size.

Some helpful links to expand on some of the subjects I mentioned.

Please let me know if you have any other questions.

-Pete

Generated at Wed Feb 07 21:42:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.