【问题标题】:Powershell Mongodb AuthenticationPowershell Mongodb 身份验证
【发布时间】:2014-12-29 20:39:40
【问题描述】:

谁能告诉我如何使用 power shell 连接 mongodb 和身份验证?我看到了本地主机连接的引用,但没有看到 mongodb 服务器连接字符串。

欢迎参考

【问题讨论】:

    标签: mongodb powershell database-connection driver


    【解决方案1】:

    这是来自 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
    }
    

    【讨论】:

    • 我认为这不适用于最新的 MongoDB 驱动程序,因为我在使用几乎相同的关于 get-collection 的脚本时遇到了同样的问题。能否提供最新驱动的解决方案?因为它实际上也会解决我的赏金问题。
    • 这是我的问题,与此类似,但我需要最新的 C# 驱动程序:stackoverflow.com/questions/31878209/…
    • 我也有最新的驱动程序,但我现在没有sn-p。
    • 以后再贴就好了
    猜你喜欢
    • 2012-10-05
    • 1970-01-01
    • 2015-08-27
    • 2019-05-15
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    相关资源
    最近更新 更多