如何将MongoDB与PowerShell连接?

时间:2017-07-10 11:34:07

标签: mongodb powershell

我已尝试使用以下代码:

$mongoDbDriverPath = '
 C:\Mongodb\net45\'
$mongoServer = 'localhost:27017'

Add-Type -Path "$($mongoDbDriverPath)MongoDB.Bson.dll"
Add-Type -Path "$($mongoDbDriverPath)MongoDB.Driver.dll"
$databaseName = "test"
$collectionName = "sample"
$client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://localhost:27017"
$server = $client.GetServer()
$database = $server.GetDatabase($databaseName)
$collection = $database.GetCollection($collectionName)
Write-Host $server,$database,$collection
$query = [MongoDB.Driver.Builders.Query]::EQ("Name", "sample")

$results = $collection.Find($query)
$results

但它显示了一些错误:

  

New-Object:异常调用" .ctor"用" 1"参数:"无法加载文件或程序集System.Runtime.InteropServices.RuntimeInformation,Version = 4.0.0.0,`Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'或其中一个依赖项。系统找不到指定的文件。"
  在D:\ Users \ xxxxxx \ Desktop \ Mongodb With Powershell \ task1.ps1:8 char:11

如何克服此错误?

2 个答案:

答案 0 :(得分:0)

TL; DR:

所以你只需找到这个程序集并将它添加到与MongoDriver相同的文件夹中。我在visual studio中创建了一个控制台应用程序并添加了Nuget的驱动程序。互操作程序集将位于packages目录中。

https://www.nuget.org/packages/mongocsharpdriver/2.5.0/

研究:

启用Fusion Logging,如下所示:

https://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx

现在初始化MongoClient

Add-Type -Path "C:\temp\Mongo\MongoDB.Bson.dll"
Add-Type -Path "C:\Temp\Mongo\MongoDB.Driver.dll"
$mongoClient = New-Object MongoDB.Driver.MongoClient

你应该在融合日志中看到类似的内容:

*** Assembly Binder Log Entry  (12/29/2017 @ 1:50:26 PM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/Windows/System32/WindowsPowerShell/v1.0/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = powershell.exe
Calling assembly : MongoDB.Driver.Core, Version=2.5.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: No application configuration file found.
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Publisher policy file is not found.
LOG: Post-policy reference: System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Runtime.InteropServices.RuntimeInformation.DLL.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.DLL.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Runtime.InteropServices.RuntimeInformation.EXE.
LOG: Attempting download of new URL file:///C:/Windows/System32/WindowsPowerShell/v1.0/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.EXE.
LOG: Attempting download of new URL file:///C:/Temp/Mongo/System.Runtime.InteropServices.RuntimeInformation.DLL.
LOG: Attempting download of new URL file:///C:/Temp/Mongo/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.DLL.
LOG: Attempting download of new URL file:///C:/Temp/Mongo/System.Runtime.InteropServices.RuntimeInformation.EXE.
LOG: Attempting download of new URL file:///C:/Temp/Mongo/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.EXE.
LOG: All probing URLs attempted and failed.

因此,基于错误,它正在加载程序集MongoDB.Driver.Core,这就是抛出异常的地方。

Reflected Mongo Assembly 所以你只需要找到这个程序集并将它添加到与MongoDriver相同的文件夹中。我在visual studio中创建了一个控制台应用程序并添加了Nuget的驱动程序。互操作程序集将位于packages目录中。

https://www.nuget.org/packages/mongocsharpdriver/2.5.0/

答案 1 :(得分:0)

我知道我来晚了一点,但是最近几天我一直在与Mongodb和Powershell一起玩。我发现最简单的解决方案是从Powershell画廊中安装MongoDB cmdlet:

https://github.com/nightroman/Mdbc

第1步:获取并安装。

  

Mdbc作为PowerShell库模块Mdbc分发。在   PowerShell 5.0或使用PowerShellGet可以通过它进行安装   命令:

     

安装模块Mdbc步骤2:在PowerShell命令提示符下,导入   模块:

     

导入模块Mdbc步骤3:看一下帮助:

     

帮助about_Mdbc

     

help Connect-Mdbc -full

然后执行以下步骤以查看设置是否正常工作:

# Load the module
Import-Module Mdbc

# Connect the new collection test.test
Connect-Mdbc . test test -NewCollection

# Add some test data
@{_id=1; value=42}, @{_id=2; value=3.14} | Add-MdbcData

# Get all data as custom objects and show them in a table
Get-MdbcData -As PS | Format-Table -AutoSize | Out-String

# Query a document by _id using a query expression
$data = Get-MdbcData (New-MdbcQuery _id -EQ 1)
$data

# Update the document, set the 'value' to 100
$data._id | Update-MdbcData (New-MdbcUpdate -Set @{value = 100})

# Query the document using a simple _id query
Get-MdbcData $data._id

# Remove the document
$data._id | Remove-MdbcData

# Count remaining documents, 1 is expected
Get-MdbcData -Count
相关问题