[英]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.