List all MongoDB databases from Linux bash terminal












2















Can you list all databases in MongoDB from a linux bash terminal? I cannot find the correct command. I am trying to list all so as that I can write a bash script to backup all DB's individually. Is there a better way?










share|improve this question























  • See this question. stackoverflow.com/questions/25947929/…

    – SqlWorldWide
    Oct 12 '17 at 10:53
















2















Can you list all databases in MongoDB from a linux bash terminal? I cannot find the correct command. I am trying to list all so as that I can write a bash script to backup all DB's individually. Is there a better way?










share|improve this question























  • See this question. stackoverflow.com/questions/25947929/…

    – SqlWorldWide
    Oct 12 '17 at 10:53














2












2








2


1






Can you list all databases in MongoDB from a linux bash terminal? I cannot find the correct command. I am trying to list all so as that I can write a bash script to backup all DB's individually. Is there a better way?










share|improve this question














Can you list all databases in MongoDB from a linux bash terminal? I cannot find the correct command. I am trying to list all so as that I can write a bash script to backup all DB's individually. Is there a better way?







mongodb-3.0 linux






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 12 '17 at 10:32









eekfonkyeekfonky

327




327













  • See this question. stackoverflow.com/questions/25947929/…

    – SqlWorldWide
    Oct 12 '17 at 10:53



















  • See this question. stackoverflow.com/questions/25947929/…

    – SqlWorldWide
    Oct 12 '17 at 10:53

















See this question. stackoverflow.com/questions/25947929/…

– SqlWorldWide
Oct 12 '17 at 10:53





See this question. stackoverflow.com/questions/25947929/…

– SqlWorldWide
Oct 12 '17 at 10:53










3 Answers
3






active

oldest

votes


















0














This is a bit tricky, as built-in command show dbs will show databases with sizes. Plus this command cannot be evaled.



However commands can be passed with bash heredoc syntax. And to get rid of sizes, only even values can be displayed.



To skip showing welcome message use --quiet option.



Resulting script:





#!/bin/bash

# $dbs will contain db names and sizes mixed together
# Use --quiet to skip connection information
dbs=$(mongo --quiet <<EOF
show dbs
quit()
EOF
)
i=0
for db in ${dbs[*]}
do
# Odd values are db names
# Even values are sizes
i=$(($i+1))
# Show db name, ignore size
if (($i % 2)); then
echo "$db"
fi
done





share|improve this answer































    0














    Use --eval parameter



    mongo --eval 'show dbs'





    share|improve this answer



















    • 2





      mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier

      – eekfonky
      Oct 12 '17 at 10:43



















    0














    "show dbs" calculate the databases sizes and that may take some time.



    You can pass this command to the mongo shell, then clean the response:



    echo "db.getMongo().getDBNames()"|mongo --quiet |tr -d [] | tr , "n"|cut -c3-| tr -d "





    share|improve this answer








    New contributor




    Angel angelbar Barrientos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.




















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "182"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f188305%2flist-all-mongodb-databases-from-linux-bash-terminal%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      This is a bit tricky, as built-in command show dbs will show databases with sizes. Plus this command cannot be evaled.



      However commands can be passed with bash heredoc syntax. And to get rid of sizes, only even values can be displayed.



      To skip showing welcome message use --quiet option.



      Resulting script:





      #!/bin/bash

      # $dbs will contain db names and sizes mixed together
      # Use --quiet to skip connection information
      dbs=$(mongo --quiet <<EOF
      show dbs
      quit()
      EOF
      )
      i=0
      for db in ${dbs[*]}
      do
      # Odd values are db names
      # Even values are sizes
      i=$(($i+1))
      # Show db name, ignore size
      if (($i % 2)); then
      echo "$db"
      fi
      done





      share|improve this answer




























        0














        This is a bit tricky, as built-in command show dbs will show databases with sizes. Plus this command cannot be evaled.



        However commands can be passed with bash heredoc syntax. And to get rid of sizes, only even values can be displayed.



        To skip showing welcome message use --quiet option.



        Resulting script:





        #!/bin/bash

        # $dbs will contain db names and sizes mixed together
        # Use --quiet to skip connection information
        dbs=$(mongo --quiet <<EOF
        show dbs
        quit()
        EOF
        )
        i=0
        for db in ${dbs[*]}
        do
        # Odd values are db names
        # Even values are sizes
        i=$(($i+1))
        # Show db name, ignore size
        if (($i % 2)); then
        echo "$db"
        fi
        done





        share|improve this answer


























          0












          0








          0







          This is a bit tricky, as built-in command show dbs will show databases with sizes. Plus this command cannot be evaled.



          However commands can be passed with bash heredoc syntax. And to get rid of sizes, only even values can be displayed.



          To skip showing welcome message use --quiet option.



          Resulting script:





          #!/bin/bash

          # $dbs will contain db names and sizes mixed together
          # Use --quiet to skip connection information
          dbs=$(mongo --quiet <<EOF
          show dbs
          quit()
          EOF
          )
          i=0
          for db in ${dbs[*]}
          do
          # Odd values are db names
          # Even values are sizes
          i=$(($i+1))
          # Show db name, ignore size
          if (($i % 2)); then
          echo "$db"
          fi
          done





          share|improve this answer













          This is a bit tricky, as built-in command show dbs will show databases with sizes. Plus this command cannot be evaled.



          However commands can be passed with bash heredoc syntax. And to get rid of sizes, only even values can be displayed.



          To skip showing welcome message use --quiet option.



          Resulting script:





          #!/bin/bash

          # $dbs will contain db names and sizes mixed together
          # Use --quiet to skip connection information
          dbs=$(mongo --quiet <<EOF
          show dbs
          quit()
          EOF
          )
          i=0
          for db in ${dbs[*]}
          do
          # Odd values are db names
          # Even values are sizes
          i=$(($i+1))
          # Show db name, ignore size
          if (($i % 2)); then
          echo "$db"
          fi
          done






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 6 '18 at 9:41









          PeterMPeterM

          1162




          1162

























              0














              Use --eval parameter



              mongo --eval 'show dbs'





              share|improve this answer



















              • 2





                mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier

                – eekfonky
                Oct 12 '17 at 10:43
















              0














              Use --eval parameter



              mongo --eval 'show dbs'





              share|improve this answer



















              • 2





                mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier

                – eekfonky
                Oct 12 '17 at 10:43














              0












              0








              0







              Use --eval parameter



              mongo --eval 'show dbs'





              share|improve this answer













              Use --eval parameter



              mongo --eval 'show dbs'






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Oct 12 '17 at 10:39









              SQLadminSQLadmin

              801426




              801426








              • 2





                mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier

                – eekfonky
                Oct 12 '17 at 10:43














              • 2





                mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier

                – eekfonky
                Oct 12 '17 at 10:43








              2




              2





              mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier

              – eekfonky
              Oct 12 '17 at 10:43





              mongo --eval 'show dbs' MongoDB shell version: 3.0.15 connecting to: test 2017-10-12T10:43:10.642+0000 E QUERY SyntaxError: Unexpected identifier

              – eekfonky
              Oct 12 '17 at 10:43











              0














              "show dbs" calculate the databases sizes and that may take some time.



              You can pass this command to the mongo shell, then clean the response:



              echo "db.getMongo().getDBNames()"|mongo --quiet |tr -d [] | tr , "n"|cut -c3-| tr -d "





              share|improve this answer








              New contributor




              Angel angelbar Barrientos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

























                0














                "show dbs" calculate the databases sizes and that may take some time.



                You can pass this command to the mongo shell, then clean the response:



                echo "db.getMongo().getDBNames()"|mongo --quiet |tr -d [] | tr , "n"|cut -c3-| tr -d "





                share|improve this answer








                New contributor




                Angel angelbar Barrientos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.























                  0












                  0








                  0







                  "show dbs" calculate the databases sizes and that may take some time.



                  You can pass this command to the mongo shell, then clean the response:



                  echo "db.getMongo().getDBNames()"|mongo --quiet |tr -d [] | tr , "n"|cut -c3-| tr -d "





                  share|improve this answer








                  New contributor




                  Angel angelbar Barrientos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.










                  "show dbs" calculate the databases sizes and that may take some time.



                  You can pass this command to the mongo shell, then clean the response:



                  echo "db.getMongo().getDBNames()"|mongo --quiet |tr -d [] | tr , "n"|cut -c3-| tr -d "






                  share|improve this answer








                  New contributor




                  Angel angelbar Barrientos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Angel angelbar Barrientos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 17 mins ago









                  Angel angelbar BarrientosAngel angelbar Barrientos

                  1




                  1




                  New contributor




                  Angel angelbar Barrientos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Angel angelbar Barrientos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Angel angelbar Barrientos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Database Administrators Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f188305%2flist-all-mongodb-databases-from-linux-bash-terminal%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Liste der Baudenkmale in Friedland (Mecklenburg)

                      Single-Malt-Whisky

                      Czorneboh