Skip to content

Instantly share code, notes, and snippets.

@swaroopsm
Created March 20, 2013 09:52
Show Gist options
  • Save swaroopsm/5203535 to your computer and use it in GitHub Desktop.
Save swaroopsm/5203535 to your computer and use it in GitHub Desktop.
Export mongodb collections data and save as json
# Copyright (c) 2013 Swaroop SM <[email protected]>
# This program is free: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# <http://www.gnu.org/licenses/>.
# This shell script is used for exporting records from MongoDB.
# It exports a collection's data into json and saves it in the directory.
#!/bin/bash
# Get current system date(DD_MM_YYYY).
d="`date +%d_%m_%Y`"
# Get current system time.
t="`date +%l_%M_%p`"
# The Database Name.
db_name=""
# Declare the collections you want to export. Eg.: ('collection1' 'collection2' 'collection3') - Note: "No Commas"
declare -a coll_name=()
if [ -d $d ]
then
echo "$d directory already exists and entering in $d directory"
cd $d
else
mkdir $d
echo "$d directory created and entering in $d directory"
cd $d
fi
mkdir $t
cd $t
for i in ${coll_name[@]}
do
file_name="$i.json"
mongoexport -d $db_name -c $i -o $file_name
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment