[英]MongoDB with Powershell
我正在以JSON格式的變量($ report)檢索VM使用率詳細信息。
$ report看起來像:
{
{
"VM_Name" : "VMtest1"
"Datastore":"KJo91"
},
{
"VM_Name" : "VMtest2"
"Datastore":"KJo91"
},so on....
}
現在,我需要通過powershell將其導入到MongoDB中。 有可能嗎?還是應該將其另存為Jsonfile並導入? 對於這兩種情況,都需要一個powershell命令。
先感謝您。
對的,這是可能的。 您需要使用Mongo C#驅動程序,然后使用標准的.NET對象方法。 檢查以下鏈接: C#和.NET MongoDB驅動程序以及將Mongodb與PowerShell配合使用
是的,我已經使用了C#MongoDB驅動程序,可以將JSON文件( Sample1.json
)導入MongoDB,例如
# 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
我沒有創建文件,而是使用一個具有JSON格式數據的變量:
# Command used
$result = $report | ConvertTo-Json -Depth 1
現在,我必須將$result
變量(包含集合/文檔)導入MongoDB。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.