【发布时间】:2014-12-29 20:39:40
【问题描述】:
谁能告诉我如何使用 power shell 连接 mongodb 和身份验证?我看到了本地主机连接的引用,但没有看到 mongodb 服务器连接字符串。
欢迎参考
【问题讨论】:
标签: mongodb powershell database-connection driver
谁能告诉我如何使用 power shell 连接 mongodb 和身份验证?我看到了本地主机连接的引用,但没有看到 mongodb 服务器连接字符串。
欢迎参考
【问题讨论】:
标签: mongodb powershell database-connection driver
这是来自 Powershell 的 MongoDb 身份验证的 sn-p。
我这里用的是MongoDB C#驱动(看看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
}
【讨论】: