Для всех, кто ищет кросс-платформенное решение, я создал небольшой скрипт perl, который сделает это. Он принимает аргумент базы данных и каталога и будет импортировать любые .json-файлы, которые он находит в каталоге mongodb. Если вы не дадите ему каталог, он просто использует ту, в которой вы сейчас находитесь. Мне нужно уточнить регулярное выражение, которое проверяет файлы .json, и я уверен, что это можно сделать с меньшим количеством кода, m новичок Perl monk), но это работает, и мне нравится Perl .. так, любому, кто находит это - наслаждайтесь.
#!/usr/bin/perl
use strict;
use warnings;
#this is a script for enumerating over every json file in a folder and importing it into mongodb
my ($database, $directoryPath) = @ARGV;
if(! $database) { #check for required database argument
die "A database argument must be provided to the script. Ex: perl mongorestore.pl wasp";
}
#if a directory path is not given in arguments, operate in the current directory.
if(!$directoryPath) {
$directoryPath = '.';
}
#open directory and import json files to mongo
opendir my $dir, $directoryPath or die "Cannot open directory at path $directoryPath.";
my @files = readdir $dir;
importJSONToMongo(@files);
closedir $dir;
#subroutine that takes an array of json files and imports them to the given mongodb database
sub importJSONToMongo {
foreach my $file (@_) {
if($file =~ /.json/) { #only import json files - need to make this regex better (it would match *.metadata.json and other extraneous files)
$file =~ /(^.+?)(?=\.)/; #capture the filename before the '.json' extension
system("mongoimport -d $database -c $1 --jsonArray --file $directoryPath/$1.json");
}
}
}