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

to find the size of a file and assign it to a variable in UNIX script
How can I start in command mode (not insert mode) in readline's vi editting-mode
how to control the timing of call to scripts within a shell wrapper
Not able to execute script with parameters within a SSH script
Adding two digital numbers
Replace value of filed IF MATCH
POST with Curl to utilize artifactory API?
Checking if directory in HDFS already exists or not
Update oracle table with start and end times from shell script
Output the test shell script status to webpage
How to run SWI-Prolog from the command line?
Search, count and position of the count in a file
Unexpected empty result from shell script that calls awk
Input a command result in a command line program
How can I execute a script that runs within an xterm/xgterm?
ssh executing nsenter as remote command with interactive shell in golang to debug docker container

Categories

HOME
uml
hyperlink
rdbms
angular-formly
oracle-apex-5
requirements
devise
parameters
vert.x
webix
title
performancepoint
fabric.io
plist
windows-ce
redis-sentinel
angular-dart
automatic-ref-counting
magnetic-cards
mongodb-csharp
symbol
powershell-v5.0
microsoft-dynamics-nav
sigma.js
lilypond
ag
jquery-ias
roslyn
pchart
dynamically-generated
ckfinder
syntax-highlighting
complex-numbers
playstation-portable
xajax
tex
release
google-data-api
dcmtk
linqpad
httrack
proget
activeperl
corruption
yii2-user
colorbar
edit
er-diagram
vibrate
polyline
change-tracking
gets
rocks
pytables
omxplayer
custom-lists
flex-monkey
drawstring
jboss-tools
i386
expectations
tracker-enabled-dbcontext
bizagi
phpwebsocket
jaunt-api
sciruby
oracle-spatial
text-search
evolus-pencil
dnssec
rrule
cosine-similarity
execve
webmatrix-3
human-computer-interface
gii
trdion2011
mobility
lnk
cancellation
vsx
square-bracket
windows-embedded-standard
artemis
mantle
cascadingdropdown
sqlclr
virtualpathprovider
convention
pinch
user-tracking
listactivity
cdonts
metaweblog
wcf-instancing

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App