Question:
Problem with latest MongoDB Driver caused by the C# Generics:
1 2 |
Cannot find an overload for "GetCollection" and the argument count: "1". |
I could probably use other GetCollection methods without the generics, but I do not know how exactly.
The answer posted further down points out some good information but unfortunately the provided code was the same than I already tried and it is not working.
Here is what I would like to do:
I want to work with PowerShell to create a few documents.
The issue I am facing now seems that “GetCollection” does not work properly. I think it has to do with the lack of support for generics in Powershell. Anyway I found some CmdLets to run generic methods. But I think this will make the code too complicated. Is there a way to get around this issue?
I saw that there are other GetCollection methods which are not based on C# Generics, but I do not understand yet how to use them in PowerShell.
Powershell Exception: Cannot find an overload for "GetCollection" and the argument count: "1"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# 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 listed above is copied (and slightly changed) from an earlier SO question: Powershell Mongodb Authentication
Any suggestion how this can be done? I assume that the code listed in the SO is based on an earlier driver version. I think that is why it is not working any more.
Full Exeption on PowerShell console:
1 2 3 4 5 6 7 |
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 |
Answer:
I hope this can still be of some help, this worked for me with the latest C# Driver and the current MongoDB RC-4.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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]) |