2014-02-19 47 views
0

是否可以在不連接到mongo shell的情況下將一個JSON文檔輸入到mongodb中並且不將輸入保存到文件中?輸入JSON到mongodb沒有文件,也沒有連接到mongod?

情況,我有一個PowerShell腳本,在變量中有JSON數據。我想只是將該變量輸入到mongo中,也許通過調用mongoimport命令?

到目前爲止,我曾嘗試:

mongoimport --db test --collection test1 {"field": "value"} 

,但它不喜歡這一點。

回答

0

我能找到比變量寫入文件,之後使用mongoimport沒有其它的辦法:它看起來像那只是爲file.json雖然

$jsonOutput = $output | ConvertTo-Json 
     Set-Content -Path "./info.json" -Encoding UTF8 -Value $jsonOutput 


    <##################################################################### 
       import into mongodb 
    #####################################################################> 

     mongoimport --host $mongohost --db $mongodbName --collection $collectionName ./$computername.json --jsonArray >$null 
     Write-Host "file written to db" 
     Remove-Item ./info.json