Mongodb – Manual recovery of dropped collection in mongodb

mongodbrecovery

Some collections were accidentally dropped from mongodb. Although the collections do not exist, in the dbpath there are 6GB of dbname.0, dbname.1,… and dbname.ns

We tried to run mongod –repair but instead of recovering the files it deleted them.

Is there a way to recover these data? maybe partially?

We are using standalone mongodb.

Thanks

Best Answer

We tried to run mongod --repair but instead of recovering the files it deleted them.

Repair and backup processes will (by design) ignore deleted data. Based on the filenames referenced in your question, you are using the MMAP storage engine. When a record is deleted in MMAP the first several bytes of the record are overwritten and the preallocated storage space is marked as available for re-use on the free list. There is a possibility you could write a custom script to try to recover useful bytes from deleted records, but there is no supported "undo" process for doing so.

Hopefully you took a backup of your data files before running --repair. Repairing MMAP data files results in a compete rebuild of your data files and would free up preallocated space used by deleted collections and documents. In any case where you are trying to recover or repair database files from a standalone MongoDB deployment, stopping your MongoDB instance and taking a file copy backup of your data files is a recommended starting point.

Is there a way to recover these data? maybe partially?

Your best option would be to restore from your most recent MongoDB backup.

In a standalone MMAP deployment without backups, the only other possibility would be writing custom data recovery scripts to try to identify and salvage deleted documents. This involves some significant guesswork and manual effort to clean up data. Deleted records will be missing some essential information such as the size of the associated BSON document, and there may be variations in internal storage depending on the provenance of your data (for example, if all data was created in a specific version of MongoDB versus data files that have been upgraded through successive major releases).

See StackOverflow: Is there any way to recover deleted documents? for some possible approaches.