PowerShell与MongoDB C#驱动程序的方法不兼容? [英] PowerShell not compatible with MongoDB C# Driver's Methods?

查看:124
本文介绍了PowerShell与MongoDB C#驱动程序的方法不兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由C#泛型导致的最新MongoDB驱动程序问题:

 无法找到GetCollection的重载和参数计数:1。 

我大概可以使用其他的GetCollection方法,而不使用泛型,但我不知道如何。 p>

答案公布后指出了一些很好的信息,但不幸的是,提供的代码与我已经尝试过的代码相同,并且不起作用。



以下是我想要做的:



我想使用PowerShell创建一些文档。
我现在面临的问题似乎是GetCollection无法正常工作。我认为这与Powershell中对泛型的支持不足有关。无论如何,我发现一些CmdLets运行通用方法。但我认为这会使代码太复杂。有没有办法解决这个问题?



我看到有其他GetCollection方法不基于C#泛型,但我不明白如何使用他们在PowerShell中。



Powershell异常:无法找到GetCollection的重载和参数计数:1

 #Mongo DB驱动程序
Add-Type -Path'CSharpDriver-2.0.1\MongoDB.Bson。 dll'
Add-Type -Path'CSharpDriver-2.0.1\MongoDB.Driver.dll'

#连接到MongoDB
$ connectionString =mongodb:// localhost :27018
$ db =TestDB
$ collection =Test


函数Get-MongoDBCollection($ connectionString,$ db,$ collection)
{
$ mongoClient =新物体MongoDB.Driver.MongoClient($的connectionString)
$ mongoDatabase = $ mongoClient.GetDatabase($分贝)
$ mongoCollection = $ mongoDatabase.GetCollection( $ collection)
return $ mongoCollection
}


#连接到MongoDB并获得集合
$ mongoCollection = Get-MongoDBCollection $ connectionString $ db $ collection

上面列出的代码是从以前的SO问题中复制(并稍微更改): Powershell Mongodb身份验证



任何建议如何做到这一点?我假定SO中列出的代码是基于较早的驱动程序版本。我认为这就是为什么它不再工作。



PowerShell控制台上的完全执行:

 无法找到GetCollection的重载和参数计数:1。 
在F:\PowerShell\CreateDB.ps1:31 char:3
+ $ mongoCollection = $ mongoDatabase.GetCollection($ collection)
+ ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 〜
+ CategoryInfo:NotSpecified:(:) [],MethodException
+ FullyQualifiedErrorId:MethodCountCouldNotFindBest


解决方案

我希望这仍然可以得到一些帮助,这对我来说是最新的C#驱动程序和当前的MongoDB RC-4。

 函数Get-MongoDBCollection {
Param(
$ database,
$ CollectionName,
$ settings = $ null, #[MongoDB.Driver.MongoCollectionSetting]
$返回类型= [PSOBJECT]

$方法= $ 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])


Problem with latest MongoDB Driver caused by the C# Generics:

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"

# 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:

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

解决方案

I hope this can still be of some help, this worked for me with the latest C# Driver and the current MongoDB RC-4.

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])

这篇关于PowerShell与MongoDB C#驱动程序的方法不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆