我已经尝试过以下代码:
$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
我该如何克服这个错误?
我知道我有点晚了,但过去几天我一直在使用 Mongodb 和 Powershell。我发现的最简单的解决方案是从 Powershell 库安装 MongoDB cmdlet:
https://github.com/nightroman/Mdbc https://github.com/nightroman/Mdbc
第1步:获取并安装。
Mdbc 作为 PowerShell Gallery 模块 Mdbc 进行分发。在
PowerShell 5.0 或使用 PowerShellGet 您可以通过此安装它
命令:
Install-Module Mdbc
步骤 2:在 PowerShell 命令提示符中导入模块:
Import-Module Mdbc
第三步:查看帮助:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)