Struggling to connect to my mongodb database (3T)

46 Views Asked by At

I'm new the Node.js and this is my first time working with databases on mongodb. Apparently when you run an executable in the terminal you are supposed to let it run and use another terminal to run other tasks. If I need to stop coding for the day how do I disconnect from the database and close the terminal that it was running on?

Because when I first ran the executable which is the one below it worked fine:

/Users/Khanya/mongodb/bin/mongod.exe --dbpath=/Users/Khanya/monogdb-data

But after my battery died and I switched on my computer when I tried to run this line above in the terminal I got this error in the terminal (this is only a part of it, the last bit of it):

{"t":{"$date":"2022-05-16T17:17:02.753+02:00"},"s":"I", "c":"COMMAND", "id":4784923, "ctx":"initandlisten","msg":"Shutting down the ServiceEntryPoint"}
{"t":{"$date":"2022-05-16T17:17:02.754+02:00"},"s":"I", "c":"CONTROL", "id":4784925, "ctx":"initandlisten","msg":"Shutting down free monitoring"} {"t":{"$date":"2022-05-16T17:17:02.755+02:00"},"s":"I", "c":"CONTROL", "id":4784927, "ctx":"initandlisten","msg":"Shutting down the HealthLog"} {"t":{"$date":"2022-05-16T17:17:02.755+02:00"},"s":"I", "c":"CONTROL", "id":4784928, "ctx":"initandlisten","msg":"Shutting down the TTL monitor"} {"t":{"$date":"2022-05-16T17:17:02.756+02:00"},"s":"I", "c":"CONTROL", "id":4784929, "ctx":"initandlisten","msg":"Acquiring the global lock for shutdown"} {"t":{"$date":"2022-05-16T17:17:02.758+02:00"},"s":"I", "c":"-", "id":4784931, "ctx":"initandlisten","msg":"Dropping the scope cache for shutdown"}
{"t":{"$date":"2022-05-16T17:17:02.758+02:00"},"s":"I", "c":"FTDC", "id":4784926, "ctx":"initandlisten","msg":"Shutting down full-time data capture"}
{"t":{"$date":"2022-05-16T17:17:02.759+02:00"},"s":"I", "c":"CONTROL", "id":20565, "ctx":"initandlisten","msg":"Now exiting"} {"t":{"$date":"2022-05-16T17:17:02.761+02:00"},"s":"I", "c":"CONTROL", "id":23138, "ctx":"initandlisten","msg":"Shutting down","attr":{"exitCode":100}}

When I try to connect to my database on 3T this is the error I get:

enter image description here

this is the code as well:

const mongodb = require('mongodb')
const MongoClient = mongodb.MongoClient

const connectionURL = 'mongodb://127.0.0.1:27017'
const databaseName = 'task-manager'

MongoClient.connect(connectionURL, { useUnifiedTopology: true }, (error, client) => {
if (error) {
   return console.log('Unable to connect to database')
 }

const db = client.db(databaseName)

db.collection('users').insertOne({
    name: 'Khanya', 
    age: '22'
 }, (error, result) => {
    if (error) {
        return console.log('Unable to insert user')
    }

    console.log(result.ops)
})
})

So what I could I be doing wrong that could be causing this error and how do I connect and disconnect to the database so that this error doesn't occur again?

Thanks in advance for the help.

0

There are 0 best solutions below