如何使用 PowerShell 连接 MongoDB? [英] How to connect MongoDB with PowerShell?

查看:133
本文介绍了如何使用 PowerShell 连接 MongoDB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过以下代码:

I already tried with the following code:

$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

但它显示了一些错误:

新对象:使用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

New-Object : Exception calling ".ctor" with "1" argument(s): "Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation,Version=4.0.0.0,`Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified."
At D:\Users\xxxxxx\Desktop\Mongodb With Powershell\task1.ps1:8 char:11

我该如何克服这个错误?

How do I overcome this error?

推荐答案

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

I know I am little late but I have been playing around with Mongodb and Powershell for the last couple days. The easiest solution that I have found is to install the MongoDB cmdlets from the Powershell gallary:

https://github.com/nightroman/Mdbc

第 1 步:获取并安装.

Step 1: Get and install.

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

Mdbc is distributed as the PowerShell Gallery module Mdbc. In PowerShell 5.0 or with PowerShellGet you can install it by this command:

Install-Module Mdbc 

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

Step 2: In a PowerShell command prompt import the module:

Import-Module Mdbc 

第 3 步:查看帮助:

Step 3: Take a look at help:

help about_Mdbc 
help Connect-Mdbc -full

然后通过以下步骤查看设置是否有效:

Then go through the following steps to see if the setup is working:

# 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

这篇关于如何使用 PowerShell 连接 MongoDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆