简体   繁体   中英

MongoDB with Powershell

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.

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

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

# 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM