Blowing up your database

Don't use your production database for development! Or at least make a backup...

Well tonight I was going through and addressing the fact that I've accidentally been storing dates as strings in my database. I thought "I know, I'll fire up the clojure REPL and practice clojure and fix the database at the same time!"

Bad Ideatm

What did I do?

I wanted to re-write the creation date column of the articles, but instead I wrote back an object that only contained the date and the row id, effectively deleteing all the content of all the posts.

Oh.. oh dear.

> db.blogarticles.find()
{ "_id" : ObjectId("57bea32f38b571069a9cb17b"), "created" : ISODate("2016-08-25T07:49:45Z") }
{ "_id" : ObjectId("57c07039fb46bd6e18b43b65"), "created" : ISODate("2016-08-26T16:36:44Z") }
{ "_id" : ObjectId("57c74eff521c74e161ce66b7"), "created" : ISODate("2016-09-02T03:55:00Z") }
{ "_id" : ObjectId("57ca01528f5cf603751a597a"), "created" : ISODate("2016-09-02T22:43:39Z") }
{ "_id" : ObjectId("57c9fbbc8f5cf603751a5978"), "created" : ISODate("2016-09-02T22:22:35Z") }
{ "_id" : ObjectId("57cfa15538b571073df303d0"), "created" : ISODate("2016-09-07T05:09:54Z") }
{ "_id" : ObjectId("57d70ef738b571175d692842"), "created" : ISODate("2016-09-12T20:22:06Z") }
{ "_id" : ObjectId("57d78e66c606418577ceec79"), "created" : ISODate("2016-09-13T05:27:19Z") }
{ "_id" : ObjectId("57d792c3c60641896855eca5"), "created" : ISODate("2016-09-13T05:31:10Z") }
{ "_id" : ObjectId("57d7987fc606418dd65aa500"), "created" : ISODate("2016-09-13T05:31:10Z") }
{ "_id" : ObjectId("57d798b3c606418e0d607184"), "created" : ISODate("2016-09-13T05:31:10Z") }
{ "_id" : ObjectId("57d79a88c606418e85614e3f"), "created" : ISODate("2016-09-13T06:19:25Z") }
{ "_id" : ObjectId("57d79ab4c606418e85614e41"), "created" : ISODate("2016-09-13T06:19:25Z") }
{ "_id" : ObjectId("57d79b34c606418e85614e43"), "created" : ISODate("2016-09-13T06:22:31Z") }
{ "_id" : ObjectId("57d79b6ec606418e85614e45"), "created" : ISODate("2016-09-13T06:22:31Z") }
{ "_id" : ObjectId("57d79b94c606418e85614e47"), "created" : ISODate("2016-09-13T06:24:11Z") }
{ "_id" : ObjectId("57d85434c60641e10971e3a3"), "created" : ISODate("2016-09-13T19:31:42Z") }
{ "_id" : ObjectId("57d8e2ddc60641598233a905"), "created" : ISODate("2016-09-14T05:40:22Z") }
{ "_id" : ObjectId("57d8e38cc60641598233a90a"), "created" : ISODate("2016-09-14T05:43:22Z") }
{ "_id" : ObjectId("57d1805038b5710a02e9d894"), "created" : ISODate("2016-09-08T15:10:12Z") }

How did I recover?

The only thing that saved me from losing everything was that I had gotten one page with of article entries to remind myself what I was dealing with. I copied and pasted the output on the mongo console and saved it to a temp file! 😂

I then re-inserted each item via the mongo console. Sadly, if you have more than a "page" worth of results, you have to iterate (e.g. pagination) through the rest. I had not done that and so I lost some entries.

In the Future

Don't use the production database or at least back it up