Search code examples
mongodbmongoexport

mongoexport does not write any records to json output file


I have tried to export a json file from MongoDB with mongoexport in the following way:

$ mongoexport --db db --collection ds --dbpath ~/db --out ds.json
exported 0 records
Sat Apr 20 23:13:18 dbexit: 
Sat Apr 20 23:13:18 [tools] shutdown: going to close listening sockets...
Sat Apr 20 23:13:18 [tools] shutdown: going to flush diaglog...
Sat Apr 20 23:13:18 [tools] shutdown: going to close sockets...
Sat Apr 20 23:13:18 [tools] shutdown: waiting for fs preallocator...
Sat Apr 20 23:13:18 [tools] shutdown: closing all files...
Sat Apr 20 23:13:18 [tools] closeAllFiles() finished
Sat Apr 20 23:13:18 [tools] shutdown: removing fs lock...
Sat Apr 20 23:13:18 dbexit: really exiting now

I do not understand why the created json file empty is, because the database actually contains the following data:

$ mongo
MongoDB shell version: 2.2.3
connecting to: test
> use ds
switched to db ds
> db.ds.find().pretty()
{
    "_id" : "1_522311",
    "chr" : 1,
    "kg" : {
        "yri" : {
            "major" : "D",
            "minor" : "A",
            "maf" : 0.33036
        },
        "ceu" : {
            "major" : "C",
            "minor" : "A",
            "maf" : 0.05263
        }
    },
    "pos" : 522311
}
{
    "_id" : "1_223336",
    "chr" : 1,
    "kg" : {
        "yri" : {
            "major" : "G",
            "minor" : "C",
            "maf" : 0.473214
        },
        "ceu" : {
            "major" : "C",
            "minor" : "G",
            "maf" : 0.017544
        },
        "jptchb" : {
            "major" : "C",
            "minor" : "G",
            "maf" : 0.220339
        }
    },
    "pos" : 223336
}

What did I do wrong?

Thank you in advance.


Solution

  • It appears that you have a database called ds:

    > use ds
    switched to db ds
    

    use ds switches the current database to the ds database (db from the shell is just an alias for the current database).

    Then, you have a collection called ds as well:

    > db.ds.find().pretty()
    

    So, that means you have a ds database with a ds collection (ds.ds).

    You should then use the export like this with the --db option set to ds (assuming the path to the database is correct):

    mongoexport --db ds --collection ds --dbpath ~/db --out ds.json
    

    3.0+ Update: --dbpath is unavailable.