如何使用PowerShell连接MongoDB?

Dha*_*n M 8 powershell mongodb

我已经尝试过以下代码:

$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
Run Code Online (Sandbox Code Playgroud)

但它显示了一些错误:

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

我该如何克服这个错误?

Hus*_*oms 5

我知道我有点晚了,但过去几天我一直在使用 Mongodb 和 Powershell。我发现的最简单的解决方案是从 Powershell 库安装 MongoDB cmdlet:

https://github.com/nightroman/Mdbc

第1步:获取并安装。

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

Install-Module Mdbc 
Run Code Online (Sandbox Code Playgroud)

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

Import-Module Mdbc 
Run Code Online (Sandbox Code Playgroud)

第三步:查看帮助:

help about_Mdbc 
help Connect-Mdbc -full
Run Code Online (Sandbox Code Playgroud)

然后执行以下步骤来查看设置是否有效:

# 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
Run Code Online (Sandbox Code Playgroud)


Mik*_*zie 1

长话短说:

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

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

研究:

启用融合日志记录,如下所述:

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
Run Code Online (Sandbox Code Playgroud)

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

*** 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.
Run Code Online (Sandbox Code Playgroud)

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

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

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