shell


creating a shell script to automate csv export in mongo db


We have mongo db and in that we have a list of collections which i wanna export to csv using the mongoexport tool. I need to do this often and the names of the collections changes sometimes. So what i wanna do is create a shell script that i can just run and it will iterate over the collections in the mongo db and create csv files. Right now i have a script but its not automated for example i have the following in a script.
mongoexport -d mydbname -c mycollname.asdno3rnknlasfkn.collection --csv -f field1,field2,field3,field4 -o mycollname.asdno3rnknlasfkn.collection.csv
In this all the elements will remain same except csv filename and the collection name where both are same.
So i wanna create a script which will
show collections
then loop over the collection names retrieved and replace it in the export tool command.
You can not call "show collections" through mongo from the shell.
I suggest you write a small skript/program using your favorite language
fetching the collection names through the driver's API and then execute
mongoexport through your script/program using a system call (system()).
This can easily be done via the shell - don't know if the comments above refer to old versions of the mongo shell...
Example:
echo 'show collections' | mongo dbname --quiet
#############################################################
Script 1 -- to produce a list of databases in MongoDB server
#############################################################
#!/bin/bash
####################################################################
# PROGRAM: mongolistdbs.sh
#
# PROGRAMMER: REDACTED
# PURPOSE: Program created to list databases in Mongo
#
# CREATED: 02/14/2011
#
# MODIFCATIONS:
###################################################################
#set -x
#supply mongo connection parms: server and port
mongo localhost:12345 --quiet <<!! 2>&1
show dbs
!!
########################################################
Script 2 -- This is the driver that calls script 1
#########################################################
####################################################################
# PROGRAM: mongodb_backup_driver.sh
#
# PROGRAMMER: REDACTED
# PURPOSE: Program created to drive the MongoDB full database
# backups
# CREATED: 02/14/2011
#
# MODIFCATIONS:
###################################################################
#!/bin/bash
################################################
# backup strategy is individual backup
# of all databases via loop and db list
# (dbparm is empty string)
###############################################
echo "Strategy: All via Individual Loop"
####################################
### here is the call of script 1
####################################
DBs=`./mongolistdbs.sh | cut -f1 | egrep -v ">"`
for db in $DBs;
do
echo "Driver loop for backup of [ ${db} ]"
#############################################################
### here im calling my backup script (not supplied)
### with database as a parameter within a loop
### to backup all databases individually
#############################################################
./royall_mongodb_backup.sh ${db}
done

Related Links

How to send a binary (ebcdic format) file with variable blocks (each record has variable length), from a unix system to a unix mainframe through NDM?
Delete first and last character from from each line of a txt file
Shell script for cron to send an email if the command produces any output
Teamcity with Subversion post commit script on windows
shell: how to pagination file with code highlight?
Read and print Line between two tags
Whats the difference between $ExecutionContext.SessionState.Path.CurrentLocation and $pwd in Powershell?
Pro*C unable to read parameter passing from korn shell script
No environment variables are set after sourcing. Vagrant provisioning with Ansible and shell
How to set PATH of SGE in cronjob
Unix terminal, “cd ..” for a specific number of directories
Sonar.sh script is getting stopped immediately in Centos Docker Continer
How to calculate the difference in date time on Sun OS?
How to insert tab in awk system() command string
shell script - AWS s3 bucket, find last added file
Shell script to create loop for command

Categories

HOME
c++11
time-complexity
codenvy
redux-form
formal-languages
twig
pclxl
sublimetext
grafana
yandex
simulink
big-o
visual-studio-emulator
limit
interpolation
maxscript
zip
tvos
missing-data
frontend
flume
opengl-es-3.0
pybossa
template-engine
question2answer
named-entity-recognition
exit
pchart
jackson-modules
has-and-belongs-to-many
nstimer
oculus
genexus-sd
bioinformatics
google-earth
complex-numbers
kodi
rselenium
github3.py
gojs
getlasterror
sim-toolkit
google-earth-engine
diacritics
p4merge
hackintosh
odoo-website
android-ibeacon
jide
dynamic-linq
automapper-5
corruption
static-cast
truezip
arules
cudd
pytables
autosar
actor-platform
async.js
iotivity
resourcemanager
avspeechsynthesizer
lean
data-protection
cortex-m
toast
evolus-pencil
apache-commons-daemon
canvg
nsurlsessiondatatask
osc
android-cookiemanager
genymotion-call
mintty
malformedurlexception
git-repo
scriptaculous
jammer
css-reset
fb.ui
mkv
vfs
usertype
winmain
google-floodlight
rmdir
creole
uipagecontrol
collectionviewsource
metaweblog
openfaces

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App