How to export collection to CSV in MongoDB?

前端 未结 11 1543
刺人心
刺人心 2020-12-12 11:52

How do you export all the records in a MongoDB collection to a .csv file?

mongoexport --host localhost --db dbname --collection name --type=csv          


        
相关标签:
11条回答
  • 2020-12-12 12:23

    For all those who are stuck with an error.

    Let me give you guys a solution with a brief explanation of the same:-

    command to connect:-

    mongoexport --host your_host --port your_port -u your_username -p your_password --db your_db --collection your_collection --type=csv --out file_name.csv --fields all_the_fields --authenticationDatabase admin
    

    --host --> host of Mongo server

    --port --> port of Mongo server

    -u --> username

    -p --> password

    --db --> db from which you want to export

    --collection --> collection you want to export

    --type --> type of export in my case CSV

    --out --> file name where you want to export

    --fields --> all the fields you want to export (don't give spaces in between two field name in between commas in case of CSV)

    --authenticationDatabase --> database where all your user information is stored

    0 讨论(0)
  • 2020-12-12 12:23

    Below command used to export collection to CSV format.

    Note: naag is database, employee1_json is a collection.

    mongoexport --db naag--collection employee1_json --type csv --out /home/orienit/work/mongodb/employee1_csv_op1
    
    0 讨论(0)
  • 2020-12-12 12:25

    @karoly-horvath has it right. Fields are required for csv.

    According to this bug in the MongoDB issue tracker https://jira.mongodb.org/browse/SERVER-4224 you MUST provide the fields when exporting to a csv. The docs are not clear on it. That is the reason for the error.

    Try this:

    mongoexport --host localhost --db dbname --collection name --csv --out text.csv --fields firstName,middleName,lastName
    

    UPDATE:

    This commit: https://github.com/mongodb/mongo-tools/commit/586c00ef09c32c77907bd20d722049ed23065398 fixes the docs for 3.0.0-rc10 and later. It changes

    Fields string `long:"fields" short:"f" description:"comma separated list of field names, e.g. -f name,age"`
    

    to

    Fields string `long:"fields" short:"f" description:"comma separated list of field names (required for exporting CSV) e.g. -f \"name,age\" "`
    

    VERSION 3.0 AND ABOVE:

    You should use --type=csv instead of --csv since it has been deprecated.

    More details: https://docs.mongodb.com/manual/reference/program/mongoexport/#export-in-csv-format

    Full command:

    mongoexport --host localhost --db dbname --collection name --type=csv --out text.csv --fields firstName,middleName,lastName
    
    0 讨论(0)
  • 2020-12-12 12:25

    Also if you want to export inner json fields use dot (. operator).

    JSON record:

    {
        "_id" : "00118685076F2C77",
        "value" : {
            "userIds" : [ 
                "u1"
            ],
            "deviceId" : "dev"
    }
    

    mongoexport command with dot operator (using mongo version 3.4.7):

    ./mongoexport --host localhost --db myDB --collection myColl --type=csv --out out.csv --fields value.deviceId,value.userIds

    Output csv:

    value.deviceId,value.userIds
    d1,"[""u1""]"
    d2,"[""u2""]"
    

    Note: Make sure you do not export an array. It would corrupt the CSV format like field userIds shown above

    0 讨论(0)
  • 2020-12-12 12:27

    Also, you are not allowed spaces between comma separated field names.

    BAD: -f firstname, lastname

    GOOD: -f firstname,lastname

    0 讨论(0)
提交回复
热议问题