1

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.

Any references are welcomed

1 Answer 1

2

Here is a snippet of MongoDb authentication from Powershell.

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

I assume this does not work with the latest MongoDB driver, since I had the same issue with almost the same script about get-collection. Can you provide a solution for the latest driver? Since it would actually solve my bounty too.
This was my question which is similar to this but I need it for the latest C# driver: stackoverflow.com/questions/31878209/…
I also have it with the latest driver, but I haven't got the snippet with me now.
Would be great if you post it later

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.