Link to home
Start Free TrialLog in
Avatar of janhoedt
janhoedt

asked on

Powershell MongoDB: Get-MdbcData : Cannot bind parameter 'Collection' to the target. Exception setting "Collection"

Hi,

Please see https://www.experts-exchange.com/questions/29154913/Powershell-and-jsons-to-a-Mongodb-PS-management-mongodb-itemd.html
I seem to be able to connect to my db, but I can't see if I am connected. Database and collection exist, I can see this via MongoDB gui.
Got error below. Please advise.

$MongoDbServer = 'Myserver:27017
#$Port = '27017'

$DatabaseName = 'TestDB'
$CollectionName = 'TestCollection'
$ConnectionString = "mongodb://$MongoDb/$Databasename"


Connect-Mdbc -ConnectionString $ConnectionString -DatabaseName $DatabaseName -CollectionName $CollectionName
#No error here
Get-MdbcData -Collection $CollectionName
#Then the error
Get-MdbcData : Cannot bind parameter 'Collection' to the target. Exception setting "Collection": "Unexpected type of parameter or variable Collection."
At line:1 char:26
+ Get-MdbcData -Collection $CollectionName
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of janhoedt
janhoedt

ASKER

Thanks, $Collection has a value indeed
➜ $Collection

Database  FullName                   Name             Settings
--------  --------                   ----             --------
TestDB TesttDB ...

But the Get-MdbcData -Collection $Collection doesn't give any data
Added some sample data, but  that's an object id. I have some jsons for a specific task, how do I add them, so I can check if I can query them?
F.e. I have jsons for software intake process, jsons for configs, jsons for software processing etc
Sorry, as I said, I'm not using MongoDb myself, so I can only help with the PowerShell based part.
Found the https://github.com/oze4/PsMongo which apparently enables to write jsons to mongodb.
Setup works, but can't add jsons

Add-JsonDocumentIntoMongoCollection -JsonData $jsonData -MongoConnection $MongoConnection -CollectionName $Collection
Error is Something went wrong saving document to database '' in collection ''! Full Error: Exception calling "JSONtoBSON" with "1" argument(s): "Cannot deserialize a 'BsonDocument' from BsonType 'Array'."
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, will try that!
How ever, each json I import should be added as one entry. Each json contain all info on a specific software (vendor, Name, version). Is this way of  import then a good approach?
Can't really tell you if it's a good approach, but that approach will only import one PS json object at a time.
First one failed, also 2nd command, error is:

'Something went wrong saving document to database '' in collection ''! Full Error: Exception calling "JSONtoBSON" with "1" argument(s): "Cannot deserialize a 'BsonDocument' from BsonType 'Array'."'
Then I guess that your input json contains an array that Add-JsonDocumentIntoMongoCollection can't handle.
This is straying too far from the original (resolved) question, into plain MongoDB territory, where I can't be of any real help, I'm afraid.