【发布时间】:2021-11-02 20:26:04
【问题描述】:
我正在尝试使用 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]]] 不包含名为 '寻找'。
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)
【问题讨论】:
标签: c# mongodb powershell collections