【问题标题】:How to connect MongoDB with PowerShell?如何将 MongoDB 与 PowerShell 连接?
【发布时间】:2017-12-14 02:58:44
【问题描述】:

我已经尝试过以下代码:

$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:使用“1”参数调用“.ctor”的异常:“无法加载文件或程序集'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

我该如何克服这个错误?

【问题讨论】:

  • 确保使用在 .NET 4.0 CLR(PowerShell 3.0 或更高版本)上运行的 PowerShell 版本。将$PSVersionTable 的输出发布到您的问题
  • PSVersion : 5.0.10586.962 CLRVersion : 4.0.30319.42000 我使用的是上述版本的powershell
  • 您找到解决方案了吗?我有同样的问题。

标签: mongodb powershell


【解决方案1】:

tl;博士:

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

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,这就是引发异常的地方。

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

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

【讨论】:

    【解决方案2】:

    我知道我迟到了,但过去几天我一直在玩 Mongodb 和 Powershell。我找到的最简单的解决方案是从 Powershell 库安装 MongoDB cmdlet:

    https://github.com/nightroman/Mdbc

    第 1 步:获取并安装。

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

    Install-Module Mdbc 
    

    第 2 步:在 PowerShell 命令提示符中导入模块:

    Import-Module Mdbc 
    

    第 3 步:查看帮助:

    help 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
    

    【讨论】:

    • 如何使用这个 Mdbc 模块使用 mongo db 查询检索数据?
    猜你喜欢
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 2020-05-12
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    相关资源
    最近更新 更多