简体   繁体   English

来自PowerShell的mongoimport

[英]mongoimport from PowerShell

I am trying to write a PowerShell script that watches a folder so that whenever a new JSON file is added to the folder or any of its sub folders a mongoimport command run to import that JSON file in a MongoDB. 我正在尝试编写一个监视文件夹的PowerShell脚本,以便每当将一个新的JSON文件添加到该文件夹​​或其子文件夹中时,就会运行mongoimport命令以将该JSON文件导入MongoDB中。 I wrote the following script, but it does not seem to do the job. 我编写了以下脚本,但似乎无法完成任务。 I feel that I have a problem in the Invoke-Command part. 我觉得在“ Invoke-Command部分有问题。

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\git\DataChain\UploadStation\to"
$watcher.Filter = "*.json"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = {
    $path = $Event.SourceEventArgs.FullPath
    Invoke-Command -FilePath "C:\Program Files\MongoDB\Server\3.4\bin\mongoimport.exe" -ArgumentList (db myDB collection jsonFiles type json file $path)
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {Start-Sleep 5}

Instead of Invoke-Command use the call operator ( & ) and splat the arguments: 而不是Invoke-Command使用调用运算符& )和图示的参数:

$params = '--db', 'myDB',
          '--collection', 'jsonFiles',
          '--type', 'json',
          '--file', $Event.SourceEventArgs.FullPath
& "C:\Program Files\MongoDB\Server\3.4\bin\mongoimport.exe" @params

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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