20210914 - `node xxx.js MY_PASSWORD` caused errors
2021-09-14 00:00:00

A review of connecting to mongodb

step 1: Create a new cluster

step 2: Select service + region

I tried to save data to mongodb. After double checking the password, the IP whitelist and that the connection string was correct, I still got these errors even if I succeeded in creating collections but unfortunately no data was saved to it.

Untitled

save section:

1
2
3
4
note.save().then(result => {
console.log('note saved!')
mongoose.connection.close()
})

mongodb dashboard:

Untitled

Then I followed the steps on Telegram and add catch() to catch errors

save section:

1
2
3
4
note.save().then(result => {
console.log('note saved!')
mongoose.connection.close()
}).catch(err => console.log(err))

But still one error left and no data in mongodb dashboard:

Untitled

Eventually I found the solution that successfully saved the data to collection:

Anyone else who get this warning when connecting to mongoDB cluster just change the driver version from 3.* to 2.* From connect > connect your application > version select 2.2.* or later

Untitled

So the url section turned to this:

1
const url = `mongodb://fullstack:${password}@cluster0-shard-00-00.xfjup.mongodb.net:27017,cluster0-shard-00-01.xfjup.mongodb.net:27017,cluster0-shard-00-02.xfjup.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-rob6kd-shard-0&authSource=admin&retryWrites=true&w=majority`

It worked!

Untitled

Untitled

catch() is not a must for this case

And I noticed the INDEXES TOTAL SIZE decreased to 4KB after I tried one more time. I’m not sure if it’s a bug because the collection still said the Index Size was 20KB.

Untitled

Untitled

Alright, it turned out to be a bug…