I balls’ed up on Prod, and learnt a new skill — MongoDB Compass
I use mongoDB and the MERN stack more generally while I am building:
everyhour - the honest way to track your time and progress
Struggling with time? everyhour helps you easily record what you do - motivating you to not waste time, prioritise what…
I couldn’t recommend the stack enough — especially if you are new to learning to code.
I spotted a bug with my most recent release…
The problem — 08/04/21 was the most important date?
It was appearing at the top of everywhere for everyone…
My first though was, “Oh good lord, no, not more timezone pain or something”. Turns out no. After a lot of hair pulling, I by chance spotted this:
Can you see it? The date string is missing a zero?!
The fix — part 1
// Months are indexed 0-11
const month = date.getMonth() < 9 ? "0" + (date.getMonth() + 1) : date.getMonth();// Dates are indexed 1-31
const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
Note: the bold 10 — this was a 9 before…
This bug was only affecting the 9th day each month, though luckily I did spot it before
The fix — part 2
But then I needed to update all of the user records that were incorrectly saved as
2021-04-9. A side from building an API to just do this single action I was a bit clueless…
A while ago I downloaded MongoDB Compass:
The GUI for MongoDB. Visually explore your data. Run ad hoc queries in seconds. Interact with your data with full CRUD…
I love it — a simple and easy way to access your DB. It has helped me learn to use the query language better, have confidence to understand the structure and is generally a nice app.
Top tip: After you first log-in make sure to add your credentials connection string (SRV) as a favourite.
Inside of Compass there was my saviour:
Right at the bottom of the app, hidden away is a console or shell or command line. Whatever it is called it is great.
Expanding it I can run queries directly on my DB. Suddenly, what may have been an afternoon of building a one time use API or manually going through many records in the db and updating them, my time came back to me.
Obviously, be careful — you are running commands on prod! Run it on you pre-production env to make sure it works first. With great power comes great responsibility, or, as our ozzie friends say, don’t be a dick mayte.