我正在尝试使用下面的代码片段使用powershell阅读mongodb5.0中的一个集合的文档。
我收到了最后一行$collection.find($query)的错误
方法调用失败,因为[MongoDB.Driver.MongoCollectionImpl`1[System.Management.Automation.PSObject,System.Management.Automation、Version=3.0.0.0、Culture=neutral、PublicKeyToken=31bf3856ad364e35]]不包含名为“Find”的方法。
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))
}
$mongoDbDriverPath = 'C:\Users\testuser\'
$mongoServer = 'localhost:27017'
Add-Type -Path "$($mongoDbDriverPath)MongoDB.Bson.dll"
Add-Type -Path "$($mongoDbDriverPath)MongoDB.Driver.dll"
add-type -path "$mongoDbDriverPath\System.Runtime.InteropServices.RuntimeInformation.dll"
Add-Type -Path "$mongoDbDriverPath\MongoDB.Bson.dll"
Add-Type -Path "$mongoDbDriverPath\MongoDB.Driver.dll"
add-type -path "$mongoDbDriverPath\DnsClient.dll";
#Add-Type -path "$mongoDbDriverPath\MongoDb.Driver.Core.dll"
Add-Type -Path "$mongoDbDriverPath\MongoDB.Libmongocrypt.dll"
Add-Type -Path "$mongoDbDriverPath\System.ValueTuple.dll"
$databaseName = 'myDatabase'
$collectionName = 'myCollection'
$client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://$mongoServer"
$database = $client.GetDatabase($databaseName)
$Collection = Get-MongoDBCollection $database $collectionName
#$Collection = Get-MongoDBCollection $database $collectionName -returnType ([MongoDB.Bson.BsonDocument])
$query = new-object MongoDB.Bson.BsonDocument('Name','john')
$collection.find($query)发布于 2021-09-05 12:55:46
Find是一种扩展方法,与C#编译器不同,PowerShell无法解决这些问题。您必须引用定义扩展方法的底层静态类型来调用它:
[MongoDb.Driver.IMongoCollectionExtensions]::Find($collection, $query)但是由于Find也是通用的,所以我们需要使用MakeGenericType()的等效方法来创建一个特定于类型的版本,我们可以调用:
function Find-MongoDbDocument
{
param(
[Parameter(Mandatory = $true, Position = 0)]
[MongoDB.Driver.IMongoCollection[MongoDB.Bson.BsonDocument]]
$collection,
[Parameter(Position = 1)]
[MongoDB.Driver.FilterDefinition[MongoDB.Bson.BsonDocument]]$filter = $null,
[ValidateRange(1,100000)]
[int]$Limit
)
$documentType = [MongoDB.Bson.BsonDocument]
# Discover the relevant Find() extension method overload
$findDefinition = [MongoDB.Driver.IMongoCollectionExtensions].GetMethods() |Where-Object {$_.Name -eq 'Find' -and $_.GetParameters().Count -eq 3} |Select-Object -First 1
# Make a type-parameter-specific version of Find()
$findBsonMethod = $findDefinition.MakeGenericMethod($documentType)
# Invoke Find()
$result = $findBsonMethod.Invoke($null, @($collection, $filter, $null))
# Limit the result set size, if requested
if($PSBoundParameters.ContainsKey('Limit')){
$result = $result.Limit($Limit)
}
# Fetch the results
$cursor = $result.ToCursor()
while($cursor.MoveNext([System.Threading.CancellationToken]::None)){
Write-Output $cursor.Current
}
}注意到,我使用[BsonDocument]而不是[psobject]作为文档类型-- MongoDB驱动程序已经对反序列化为[BsonDocument]提供了本机支持(而不是[psobject])。
使用方式如下:
# Obtain collection object
$collection = Get-MongoDBCollection $database $collectionName -returnType $([MongoDB.Bson.BsonDocument])
# Define filter
$filter = [MongoDB.Bson.BsonDocument]::new('Name', 'John')
# Fetch first 100 matching documents
Find-MongoDbDocument -Collection $collection -Filter $filter -Limit 100https://stackoverflow.com/questions/69062815
复制相似问题