Search code examples
mongodbpymongomongorestore

MongoDB still shows empty collections after restoring from dump


After mongodump, I did mongorestore which seemed to work fine

heathers-air:db heathercohen$ mongorestore -v -host localhost:27017
2015-02-06T11:22:40.027-0800 creating new connection to:localhost:27017
2015-02-06T11:22:40.028-0800 [ConnectBG] BackgroundJob starting: ConnectBG
2015-02-06T11:22:40.028-0800 connected to server localhost:27017 (127.0.0.1)
2015-02-06T11:22:40.028-0800 connected connection!
connected to: localhost:27017
2015-02-06T11:22:40.030-0800 dump/langs.bson
2015-02-06T11:22:40.030-0800    going into namespace [dump.langs]
Restoring to dump.langs without dropping. Restored data will be inserted without raising errors; check your server log
file dump/langs.bson empty, skipping
2015-02-06T11:22:40.030-0800    Creating index: { key: { _id: 1 }, name: "_id_", ns: "dump.langs" }
2015-02-06T11:22:40.031-0800 dump/tweets.bson
2015-02-06T11:22:40.031-0800    going into namespace [dump.tweets]
Restoring to dump.tweets without dropping. Restored data will be inserted without raising errors; check your server log
     file size: 4877899
30597 objects found
2015-02-06T11:22:41.883-0800    Creating index: { key: { _id: 1 }, name: "_id_", ns: "dump.tweets" }

When I try to access the data though, it's still empty and the way it looked before restore:

> show dbs
admin    (empty)
dump     0.078GB
local    0.078GB
tweets   (empty)
twitter  (empty)

It says it found 30597 objects, where did they go?


Solution

  • They went into the dump database, and then into the collections dump.tweets and dump.langs. The fact that the files are contained in the folder dump means that mongorestore thinks they should be restored to the database dump (it is inferred from the path). The verbose output even explicitly states that the data is being placed into dump.langs and dump.tweets specifically.

    If you specify the database you wish to restore to (with -d) and restore the specific files you will be able to restore the documents to the database you desire. Or, you can simply have a look in the dump database by running:

    use dump;
    db.tweets.find();
    db.langs.find();