当前位置: 首页 > 知识库问答 >
问题:

如何将MongoDB与PowerShell连接?

孟璞
2023-03-14

我已经尝试了以下代码:

$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.运行时。InteropServices.RuntimeInformation, Version=4.0.0.0,'区域性=中性, PublicKeyToken=b03f5f7f11d50a3a'或其依赖项之一。系统找不到指定的文件。"
at D:\用户\xxxxxx\Desktop\Mongodb with Powershell\task1.ps1:8 char: 11

我如何克服这个错误?

共有2个答案

广亮
2023-03-14

tl;博士:

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

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

您应该在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。驾驶员Core就是引发异常的地方。

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

梁丘安晏
2023-03-14

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

https://github.com/nightroman/Mdbc

步骤1:获取并安装。

Mdbc作为PowerShell Gallery模块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
 类似资料:
  • 问题内容: 当我使用MongoChef连接远程mongo数据库时,我使用下一个参数: 服务器 服务器: 本地主机 端口: 27017 SSH隧道 SSH地址: 10.1.0.90 端口: 25 SSH用户 名 : 用户名 SSH密码: 密码 当我与Pymongo连接时,我有以下代码: 但是我有下一个错误: 拜托,您能帮我解决这个问题吗?我做错了什么? 问题答案: 适用于我的解决方案。

  • 我在我的运行系统中有MongoDB和Robomongo,我使用Robomongo作为客户端。 我已将MongoDB安装在另一个系统上,我将其视为服务器,我想将我系统的Robomongo(作为客户端)连接到另一个系统(服务器)上的MongoDB。我应该采取哪些步骤来实现同样的目标? 我使用的是机器名,因为系统的IP地址不是静态的。但即使我使用系统的IP地址,我也会遇到同样的错误: 连接失败,无法连接

  • 我有一个web应用程序,它使用Spring Boot来处理后端逻辑。我正在尝试集成mongodb以跟踪有关此webapp用户的一些信息。我在mongodb Atlas上创建了一个数据库,通过Mongo Shell连接正常。当我尝试与Spring连接时,问题就出现了。让我给你看看所有的细节 在Atlas中,我将此IP地址(0.0.0.0/0(包括您当前的IP地址))添加到安全性中 然后我创建了一个名

  • 问题内容: 现在使用sailsjs v0.10。配置connections.js和models.js并将其更改为connection:’localMongodbServer’,已安装npm install sails-mongo。 都显示错误 如果将collections.js更改为adapter.js会显示错误 问题答案: 不看代码,我只能承担几件事。 您正在启动一个新的sailsjs v0.1

  • 我无法将作为docker容器运行的应用程序连接到mongoDB。我知道新版本docker compose不推荐使用“链接”,所以我尝试使用自定义网络。我的码头工人。yml: 我的应用程序的Dockerfile: 当我运行时,我在应用程序中得到以下错误消息: 在第一次连接时连接到服务器[127.0.0.1:27017]失败[MongoNetworkError:连接ECONNREFUSED127.0.

  • 我每天都在连接mongo db,它工作得很好,但今天它困扰着我下面的错误,任何人都可以帮我请。 连接到:测试2017-01-10T14:26:13.073 0530警告:连接到127.0.0.1:27017失败,原因:错误号:111连接被拒绝2017-01-10T14:26:13-074 0530错误:无法连接到服务器127.0-0.1:27017(127.0.0.1),在src/mongo/sh