Selective Restore in MongoDB

Avoid the slow and painful process of a full restore

When some production data is accidentally deleted and needs to be restored from backup, this is often a painful and slow process, particularly in a SQL-based database like MySQL or PostgreSQL. However, in a situation we recently ran into, the database in question was MongoDB and the restore ended up being almost trivial.

The collection in question is large, updated often, and most likely had records deleted since the last backup that should stay deleted. Looking through the options available to mongorestore [1] I came across the --filter [2] parameter. This parameter takes a mongo find() query, runs that query against the dump, and only restores records matching the query!

So, for example, to restore into the pages collection all Pages that are owned by a specific Site, the restore might look like:

mongorestore \
  --collection pages \
  --filter '{ "site_id": ObjectId("...") }' \

Running this command then restores only the records wanted, showing the record counts for confirmation:

249205 objects found
352 objects processed

In this situation, I’m glad we were running Mongo. In the future though, regardless of database system, I will be pushing more towards never actually deleting data, at least not immediately. Instead, flag records as “deleted”. Storage is cheap, and changing a single row on a record is far easier than struggling through backups!

MongoDB at OSCON by Garrett Heath is licensed under CC BY 2.0



Photo of Jason Roelofs

Jason is a senior developer who has worked in the front-end, back-end, and everything in between. He has a deep understanding of all things code and can craft solutions for any problem. Jason leads development of our hosted CMS, Harmony.