如何将MongoDB与PowerShell连接?

如何将MongoDB与PowerShell连接?,mongodb,powershell,Mongodb,Powershell,我已尝试使用以下代码: $mongoDbDriverPath='1!' C:\Mongodb\net45\n $mongoServer='localhost:27017' 添加类型-Path“$($mongoDbDriverPath)MongoDB.Bson.dll” 添加类型-Path“$($mongoDbDriverPath)MongoDB.Driver.dll” $databaseName=“测试” $collectionName=“示例” $client=新对象-TypeName Mon

我已尝试使用以下代码:

$mongoDbDriverPath='1!'
C:\Mongodb\net45\n
$mongoServer='localhost:27017'
添加类型-Path“$($mongoDbDriverPath)MongoDB.Bson.dll”
添加类型-Path“$($mongoDbDriverPath)MongoDB.Driver.dll”
$databaseName=“测试”
$collectionName=“示例”
$client=新对象-TypeName MongoDB.Driver.MongoClient-ArgumentList“mongodb://localhost:27017"
$server=$client.GetServer()
$database=$server.GetDatabase($databaseName)
$collection=$database.GetCollection($collectionName)
写入主机$server、$database、$collection
$query=[MongoDB.Driver.Builders.query]::EQ(“名称”,“示例”)
$results=$collection.Find($query)
$results
但它显示出一些错误:

新对象:使用“1”参数调用“.ctor”异常:“无法加载文件或程序集”System.Runtime.InteropServices.RuntimeInformation,版本=4.0.0.0,`Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a'或其依赖项之一。系统找不到指定的文件。“
位于D:\Users\xxxxxx\Desktop\Mongodb With Powershell\task1.ps1:8 char:11

如何克服这个错误?

tl;博士:

因此,您只需找到此程序集并将其添加到与MongoDriver相同的文件夹中。我在VisualStudio中创建了一个控制台应用程序,并从Nuget添加了驱动程序。互操作程序集将位于packages目录中

研究:

按此处所述启用Fusion日志记录:

现在初始化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
您应该在fusion日志中看到类似的内容:

*** 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相同的文件夹中。我在VisualStudio中创建了一个控制台应用程序,并从Nuget添加了驱动程序。互操作程序集将位于packages目录中


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

步骤1:获取并安装

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

步骤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

确保使用在.NET 4.0 CLR(PowerShell 3.0或更高版本)上运行的PowerShell版本。将
$PSVersionTable
的输出发布到您的问题psversion:5.0.10586.962 clrvision:4.0.30319.42000我正在使用上述版本PowerShell您找到解决方案了吗?我也有同样的问题。
# 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