nightroman / Mdbc

MongoDB Cmdlets for PowerShell
Apache License 2.0
141 stars 16 forks source link

Exception has been thrown by the target of an invocation. #59

Closed potnoddle closed 3 years ago

potnoddle commented 3 years ago

I've managed to successfully connect to a mongodb.net db and collection. However, whenever try one of the commands like Get-MdbcData there's a long timeout followed by the following error.

$conn = Connect-Mdbc -ConnectionString $connStr
$db = Get-MdbcDatabase -Name $dbName
$collection = Get-MdbcCollection -Database $db -Name "$collectionName"

works untill

$doc = Get-MdbcData @{ name = '$searchStr'} -Collection $collection

Get-MdbcData : Exception has been thrown by the target of an invocation.
At  char:8
+ $doc = Get-MdbcData @{ name = ''} -Collection $collection
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-MdbcData], TargetInvocationException
    + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,Mdbc.Commands.GetDataCommand

I'm really lost as what the issue is, I've tried adding a verbose to the command without any luck.

nightroman commented 3 years ago

Might be something wrong with the connection. By the driver design, it is lazy, so you see problems later, e.g. on Get-MdbcData.

Let's try to get some info right after connecting, e.g. list database names, can you try it?

Connect-Mdbc -ConnectionString $connStr -DatabaseName *
nightroman commented 3 years ago

I get the same (poor) error with the below script when MongoDB is not running.

Connect-Mdbc . test test
Get-MdbcData @{ name = 'bar' }

I think your case is similar (including wrong connection string).

v6.5.10 produces better errors, e.g. in my case:

Get-MdbcData : A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors =
MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster
state is { ClusterId : "1", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27017" }",
EndPoint: "Unspecified/localhost:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown",
HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ...

I close the issue. Feel free to reopen with some more details.