Skip to content

Instantly share code, notes, and snippets.

@robbeman
Last active November 29, 2019 07:34
Show Gist options
  • Save robbeman/3d2fefc89a136010438a55708c9b9905 to your computer and use it in GitHub Desktop.
Save robbeman/3d2fefc89a136010438a55708c9b9905 to your computer and use it in GitHub Desktop.
Mongo import shell script
#!/usr/bin/env bash
# Import collections in a specified folder to your mongo database
# requires 2 arguments
# 1) the database to import to
# 2) the folder containing the collections to import
# example usage
# sh mongo_import.sh dev ~/Documents/PROJECTS/example/mongo\ export
if [ -z ${1+x} ] || [ -z ${2+x} ]; then
echo "Error! This script requires two parameters:"
echo "1) the database name"
echo "2) the folder containing the collections to import"
exit 1
fi
cd "$2"
for file in ./*
do
# TODO check if it is a .json file
collection=${file:2}
collection=${collection%.json}
echo "---- Importing $collection from $file to $1"
mongoimport --db $1 --collection $collection <$file
echo "----"
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment