Hello loyal readers, I'm sure you were all trying to contact me and ask how I was doing in this absence, but sadly you can't (at least as I'm writing this). There's still no way to comment on my blog. Maybe that will change soon.
Anyway, I've been working on some typescript and react stuff, and I wanted to keep my deno progress up-to-date as well. So I thought I would follow this guide to see how to use mongoose via deno.
Sadly it assumes that you have mongodb running on your computer. To my horror, the tutorial worked!
How to find what's running on a particular port (macOS)
I'm not sure why mongo was running on my computer (or for how long), but of course I set out to kill it!
Somewhere on the interwebs to the rescue:
sudo lsof -i :<PortNumber>
In our particular case, mongodb is running on 27017 (the default mongodb port).
Kill it with fire via:
kill -9 <PID>
So now the tutorial is broken. How to restore mongodb? Docker to the rescue!
Containerizing MongoDB
The internet is full of answers, if you look for them. In this case this guy wrote about what I want.
I created a similar looking docker-compose.yaml
:
version: "3.9"
services:
mongodb:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: rootpassword
restart: always
ports:
- 27017:27017
volumes:
- mongodb_data_container:/data/db
volumes:
mongodb_data_container:
Great user and password, right? Very secure. My new friend (not really) Sony (from the post about containerizing mongo) has some great words about looking at the container, once it's running. I was too lazy to look them up myself, so Sony to the rescue:
docker ps
docker volume ls
mongosh admin -u root -p rootpassword
show dbs
use test
show collections
db.dinosaurs.find()
Great, now you should see something like the following if your application successfully connected and modified data:
[
{
_id: ObjectId("63ea9f7c3cde8df3af1d47f7"),
name: 'Deno',
description: 'The fastest and most secure dinosaur that ever lived.',
createdAt: ISODate("2023-02-13T20:37:16.886Z"),
updatedAt: ISODate("2023-02-13T20:37:16.886Z"),
__v: 0
}
]
Fixing the connection error
What's this? The tutorial isn't really full of details? That's right!
The tutorial has a line like:
await mongoose.connect("mongodb://localhost:27017");
This is fine if you don't have a username and a password, but obviously we're
super secure here. So instead you should modify your main.ts
to look like:
await mongoose.connect("mongodb://root:rootpassword@localhost:27017");
Cleanup
Ok, so now everything works, except it's not rerunnable. If you try to run main.ts
again, you'll get an error like:
error: Uncaught MongoServerError: E11000 duplicate key error collection: test.dinosaurs index: name_1 dup key: { name: "Deno" }
Of course, because we aren't cleaning up. Let's do that. As Sony pointed out, we can just connect to our mongodb instance and run commands manually.
This time I've looked at the docs to find what I want. When I do db.dinosaurs.find()
, I get:
[
{
_id: ObjectId("63eaa52ba45ac9c274eebeb9"),
name: 'Deno',
description: 'The fastest and most secure dinosaur that ever lived.',
createdAt: ISODate("2023-02-13T21:01:31.985Z"),
updatedAt: ISODate("2023-02-13T21:01:31.985Z"),
__v: 0
}
]
Kill it! (how sad 😥)
db.dinosaurs.remove( { _id: ObjectId("63eaa52ba45ac9c274eebeb9") } )
should do the job:
{ acknowledged: true, deletedCount: 1 }
Great, now main.ts
runs again:
1
Finding Deno in MongoDB --
Deno: The fastest dinosaur that ever lived.
Finding Deno (again) --
Deno: The fastest and most secure dinosaur that ever lived.
Wrapup
Wow, what great fun. Most of what I learned was more about docker and mongodb, but I suppose that's fine. It's late and I want to sleep, so I suppose that will wrap things up for today. Not a whole lot of progress on adding functionality to the blog, but I suppose any progress is good. See you tomorrow!