首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在powershell中读取MongoDB5.0集合

如何在powershell中读取MongoDB5.0集合
EN

Stack Overflow用户
提问于 2021-09-05 11:07:54
回答 1查看 305关注 0票数 0

我正在尝试使用下面的代码片段使用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”的方法。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-05 12:55:46

Find是一种扩展方法,与C#编译器不同,PowerShell无法解决这些问题。您必须引用定义扩展方法的底层静态类型来调用它:

代码语言:javascript
复制
[MongoDb.Driver.IMongoCollectionExtensions]::Find($collection, $query)

但是由于Find也是通用的,所以我们需要使用MakeGenericType()的等效方法来创建一个特定于类型的版本,我们可以调用:

代码语言:javascript
复制
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])。

使用方式如下:

代码语言:javascript
复制
# 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 100
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69062815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档