MongoDB
 SQL >> Database >  >> NoSQL >> MongoDB

Usare il driver C# di MongoDB da PowerShell

Funziona ancora con il driver C# attuale. Il trucco è la reflection, dato che PowerShell non può associare direttamente parametri di tipo generici:

function Get-MongoDBCollection { Param( $database, $CollectionName, $settings = $null, #[MongoDB.Driver.MongoCollectionSetting] $returnType = [PSOBJECT] ) $method = $database.GetType().GetMethod('GetCollection') $genericMethod = $method.MakeGenericMethod($returnType) $genericMethod.Invoke($database, [object[]]($CollectionName, $settings)) } $Collection = Get-MongoDBCollection $database 'test' # or $Collection = Get-MongoDBCollection $database 'test' -returnType ([MongoDB.Bson.BsonDocument])

Passa -returnType quando vuoi documenti fortemente tipizzati invece del default.