4

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.

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

2

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()).

1 Comment

I did something similar. I made a .js file fetching all the collections and made a shell script to catch its output and generate the export commands
2
#############################################################
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.