We help IT Professionals succeed at work.

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

Medium Priority
179 Views
Last Modified: 2019-09-20
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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
From what I can find (not using it myself), Get-MdbcData expects a collection object, not a name. A variable named "Collection" representing the collection passed to Connect-Mdbc should automatically be created by Connect-Mdbc.
So try it like this:
$MongoDbServer = 'Myserver:27017

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

Connect-Mdbc -ConnectionString $ConnectionString -DatabaseName $DatabaseName -CollectionName $CollectionName
# Variable $Collection was created by Connect-Mdbc
Get-MdbcData -Collection $Collection

Open in new window

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Sorry, as I said, I'm not using MongoDb myself, so I can only help with the PowerShell based part.

Author

Commented:
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'."
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
I'd guess your $jsonData contains a PowerShell object array, and Add-JsonDocumentIntoMongoCollection only accepts a single json object at a time.
If the cmdlet accepts the json through the pipeline, you can try
$jsonData | Add-JsonDocumentIntoMongoCollection -MongoConnection $MongoConnection -CollectionName $Collection

Open in new window

If it doesn't, try
$jsonData | ForEach-Object {Add-JsonDocumentIntoMongoCollection -JsonData $_ -MongoConnection $MongoConnection -CollectionName $Collection}

Open in new window

Author

Commented:
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?
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Can't really tell you if it's a good approach, but that approach will only import one PS json object at a time.

Author

Commented:
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'."'
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
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.