0

I'm retriving a VM utilization details in a variable($report)which is in JSON format.

$report looks something like:

{
   { 
   "VM_Name" : "VMtest1"
   "Datastore":"KJo91"
   },
   { 
   "VM_Name" : "VMtest2"
   "Datastore":"KJo91"
   },so on....
}

Now I need to import this into MongoDB via powershell. Is it possible?Or should I save it as a Jsonfile and import? For both cases required a powershell command.

Thank you in advance.

2 Answers 2

1

Yes, it is possible. You need to use Mongo C# driver and then the standard .NET object approach. Check this links: C# and .NET MongoDB Driver and Using Mongodb with PowerShell

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I have used C# MongoDB Driver.
0

Yes, I have used C# MongoDB Driver, I can import a JSON file (Sample1.json) into MongoDB, e.g.

# Command Used
$report = Get-VM #retrives the VM Details
$report | ConvertTo-Json -Depth 1 | Out-File "Path" # Here a file is saved in the specified path and I can import the file to MongoDB using the below command:
.\mongoimport.exe  --db $dbName --collection $collectionName ./sample1.json --jsonArray

Instead of creating a file, I'm using a variable which has the data in JSON format:

# Command used
$result = $report | ConvertTo-Json -Depth 1 

Now I have to import the $result variable (which contains the collection/document) into MongoDB.

Comments

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.