[CSHARP-1713] Need Powershell to access mongodb v3.x Created: 18/Jul/16  Updated: 05/Apr/19  Resolved: 18/Aug/16

Status: Closed
Project: C# Driver
Component/s: Connectivity
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Ross Jurek Assignee: Unassigned
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by CSHARP-1712 accessing mongodb v3.x with powershell Closed

 Description   

I had a powershell script that was access mongodb 2.x with the csharpdriver 1.7 from 10gen, now the mongodb is v3.x and the script is failing on credentials, I have upgraded to the latest csharpdriver and still can not gain access to the v3.x version of mongodb



 Comments   
Comment by Craig Wilson [ 20/Jul/16 ]

I'm not a powershell guru, so you're gonna have to work through this a bit on your own I'm afraid. Here is the API documentation for the class: http://api.mongodb.com/csharp/2.2/html/T_MongoDB_Driver_MongoClientExtensions.htm.

I've likely made a mistake in my powershell syntax. I'd be great to use the new API instead, but it's full of generics and I seem to recall that powershell doesn't play with generics very well.

Craig

Comment by Ross Jurek [ 19/Jul/16 ]

I get an error that MongoClientExtensions does not exist.

Comment by Craig Wilson [ 18/Jul/16 ]

Ross,

I think if you add

$server = [MongoClientExtensions]::GetServer($client)

before

$database = $client.GetDatabase($dbName)

Your code should continue to work. In the new API, GetCollection is a generic method.

If you'd like to see better support for powershell from the .NET Driver team, please create a new ticket with the request. So far, there hasn't been a whole lot of interest.

Craig

Comment by Ross Jurek [ 18/Jul/16 ]

------ driver 1.7 from 10gen, mongo 2.6 - This code gives a credential error when changing to the 3.2.1 mongodb use old driver, changed to new driver, error that create method does not exist.
$mongoDbDriverPath = "C:\utilities\mongoDB_1\"
$dbName = "Prod"
$collectionName = "Collect"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.Core.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Bson.dll"
$db = [MongoDB.Driver.MongoDatabase]::Create("mongodb://user:password@server/$($dbname)")
$collection = $db[$collectionName]
$query = [MongoDB.Driver.Builders.Query]::AND(
[MongoDB.Driver.Builders.Query]::EQ("Result","Response status code does not indicate success: 403 (Forbidden)."),
[MongoDB.Driver.Builders.Query]::EQ("ItemName","Automatic but Stopped Services"))
$results = $collection.find($query)

------ 2.6 csharpdriver, mongodb 3.2.1 - get Cannot find an overload for "GetCollection" and the argument count: "1".

$mongoDbDriverPath = "C:\utilities\mongoDB_2\"
$dbName = "Prod"
$collectionName = "Collect"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.Core.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Bson.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.GridFS.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.Legacy.dll"
$client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://user:password@server"

$database = $client.GetDatabase($dbName)
$collection = $database.GetCollection('collect')

$query = [MongoDB.Driver.Builders.Query]::AND(
[MongoDB.Driver.Builders.Query]::EQ("Result","Response status code does not indicate success: 403 (Forbidden)."),
[MongoDB.Driver.Builders.Query]::EQ("ItemName","KO - Automatic but Stopped Services"))

$results = $collection.find($query)

Comment by Craig Wilson [ 18/Jul/16 ]

Hi Ross,

Sorry you are having some trouble. In order to help, we need to see your script. I'd also like to know exactly what version of MongoDB you were running and exactly what version of MongoDB you are now running. In addition, I need to know the exact version of the driver you are unable to connect with.

Craig

Generated at Wed Feb 07 21:40:27 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.