Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How To Export a MongoDB Database on Ubuntu 14.04?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 323
    Comment on it

    Exporting Information From MongoDB

     

    You can export MongoDB and acquire human readable text file. Data is exported in json format by default, but you can also export MongoDB in csv format. If you want to export data from MongoDB then you can use the command mongoexport.

    Example: Lets export restaurants information from newdb database. It can be done by typing:

     

    sudo mongoexport --db newdb -c restaurants --out newdbexport.json

     --db:  to specify the database

    -c for the collection 

    --out for the file in which the data will be saved.

    Following is the output of mongoexport

    Output of mongoexport

    2016-04-17T03:39:00.143-0500    connected to: localhost
    2016-04-17T03:39:03.145-0500    exported 26000 records

    The above output shows that 26000 documents have been imported.

    If you want to export data for only some part of your collection then you need to connect to the database again. Here we will export all the restaurants which satisfies the following criteria to be situated in Bronx borough and to have Chinese cuisine.

     

    sudo mongo newdb


    Then, use this query:

    db.restaurants.find( { borough: "Bronx", cuisine: "Chinese" } ) 

    The results are displayed to the terminal. To exit the MongoDB prompt, type exit at the prompt:

     

    exit

     

    Instead of connected to the database, if you want to export data from sudo command then make  the previous query part of the mongoexport command by specifying it for the -q argument like this:

     

    sudo mongoexport --db newdb -c restaurants -q "{ borough: 'Bronx', cuisine: 'Chinese' }" --out Bronx_Chinese_retaurants.json 

     

    If the export has been successful, the result should look like this:

    Output of mongoexport

    2016-04-17T04:16:28.381-0500    connected to: localhost
    2016-04-17T04:16:28.461-0500    exported 300 records

    300 records has been exported as shown above, and you can find it in Bronx_Chinese_retaurants.json file which we have specified.

     

    Thanks for reading the blog.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: