PowerShell not compatible with MongoDB C # driver methods?
Issue with latest MongoDB driver caused by C # Generics:
Cannot find an overload for "GetCollection" and the argument count: "1".
Perhaps I could use other non-generic GetCollection methods, but I don't know how exactly.
The answer posted below provides some good information, but unfortunately the code provided was the same as I already tried and it doesn't work.
Here's what I would like to do:
I want to work with PowerShell to create multiple documents. The problem I am currently facing seems to be that "GetCollection" is not working correctly. I think this is due to the lack of generics support in Powershell. Anyway, I found some CmdLets to run common methods. But I think this will make the code too complex. Is there a way to get around this problem?
I have seen that there are other GetCollection methods that are not based on C # Generics, but I do not yet understand how to use them in PowerShell.
Powershell Exception: Cannot find an overload for "GetCollection" and the argument count: "1"
# Mongo DB driver
Add-Type -Path 'CSharpDriver-2.0.1\MongoDB.Bson.dll'
Add-Type -Path 'CSharpDriver-2.0.1\MongoDB.Driver.dll'
# Conncetion to MongoDB
$connectionString = "mongodb://localhost:27018"
$db = "TestDB"
$collection = "Test"
function Get-MongoDBCollection ($connectionString, $db, $collection)
{
$mongoClient = New-Object MongoDB.Driver.MongoClient($connectionString)
$mongoDatabase = $mongoClient.GetDatabase($db)
$mongoCollection = $mongoDatabase.GetCollection($collection)
return $mongoCollection
}
# Connect to MongoDB and get collection
$mongoCollection = Get-MongoDBCollection $connectionString $db $collection
The code above is copied (and slightly modified) from an earlier SO question: Mongodb Powershell Authentication
Any suggestion how this can be done? I am assuming that the code given on SO is based on an earlier version of the driver. I guess that's why it doesn't work anymore.
Hard uninstall in PowerShell console:
Cannot find an overload for "GetCollection" and the argument count: "1".
At F:\PowerShell\CreateDB.ps1:31 char:3
+ $mongoCollection = $mongoDatabase.GetCollection($collection)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
source to share
Hope this can still help, it worked for me with the latest C # driver and current MongoDB RC-4.
function Get-MongoDBCollection {
Param(
$database,
$CollectionName,
$settings = $null, #[MongoDB.Driver.MongoCollectionSetting]
$returnType = [PSOBJECT]
)
$method = $database.GetType().GetMethod('GetCollection')
$gericMethod = $method.MakeGenericMethod($returnType)
$gericMethod.Invoke($database,[object[]]($CollectionName,$settings))
}
$Collection = Get-MongoDBCollection $database 'test'
# or
$Collection = Get-MongoDBCollection $database 'test' -returnType ([MongoDB.Bson.BsonDocument])
source to share
Have you considered proxying the mongo call through some custom .Net code and then loading that .NET assembly into Powershell? You can have overloads in your C # code to work with different types.
Here you can use the steps here to download a custom assembly.
Then in your custom method, you can convert generic types to types Powershell can understand.
source to share
I think you have already answered this question with some statements in your question:
I think this is due to the lack of generics support in Powershell
and
the code given in SO is based on an earlier version of the driver. I guess that's why it doesn't work anymore
This link confirms what you said above and also provides some feedback on the v2 driver.
Below is an excerpt from the homepage , there is an assumption that the old driver is still available:
you can use driver version 1.10
So maybe you better find an older version and continue with it.
EDIT:
In case this is a problem with the new API, it looks like the old version is still supported. From the link above, maybe you should try:
function Get-MongoDBCollection ($connectionString, $db, $collection)
{
$mongoClient = New-Object MongoDB.Driver.MongoClient($connectionString)
$mongoServer = $mongoClient.GetServer()
$mongoDatabase = $mongoServer.GetDatabase($db)
$mongoCollection = $mongoDatabase.GetCollection($collection)
return $mongoCollection
}
source to share