Powershell Mongodb身份验证 [英] Powershell Mongodb Authentication

查看:341
本文介绍了Powershell Mongodb身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何使用power shell连接mongodb身份验证?我看到与本地主机连接的引用,但没有看到mongodb服务器连接字符串。

can anyone tell me how to connect mongodb with authentication using power shell? I see references with local host connection but don't see mongodb server connection string.

欢迎任何引用

推荐答案

这里是一个来自Powershell的MongoDb身份验证的代码片段。

Here is a snippet of MongoDb authentication from Powershell.

我在这里使用MongoDB C# http://docs.mongodb.org/ecosystem/tutorial/authenticate-with-csharp-driver/ =nofollow>此处)

I use here MongoDB C# driver (have a look here)

# Mongo DB driver
Add-Type -Path 'C:\Path_To_mongocsharpdriver\mongocsharpdriver.1.9.2\lib\net35\MongoDB.Bson.dll'
Add-Type -Path 'C:\Path_To_mongocsharpdriver\mongocsharpdriver.1.9.2\lib\net35\MongoDB.Driver.dll'

# Connexion to MongoDB
$connectionString = "mongodb://user1:password1@localhost"
$db =  "MyDBName"
$collection =  "MyCollectionName"

function Get-MongoDBCollection ($connectionString, $db, $collection)
{
  $mongoClient = New-Object MongoDB.Driver.MongoClient($connectionString)
  $mongoServer = $mongoClient.GetServer()
  $mongoDatabase = $mongoServer.GetDatabase($db)
  $mongoCollection = $mongoDatabase.GetCollection($collection)
  return $mongoCollection
}

$FileName = $args[0]
# get the file name 
$FileNameLeaf = Split-Path $FileName -Leaf

# Connect to MongoDB and get collection
$mongoCollection = Get-MongoDBCollection $connectionString $db $collection

# Verify if this file is integrated
$query = New-Object MongoDB.Driver.QueryDocument('Fic_Data', $FileNameLeaf)
$found = $mongoCollection.FindOne($query)
if ($found -ne $null)
{
  Write-Host "`tThe file $FileNameLeaf is integrated !"
  return
}

这篇关于Powershell Mongodb身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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