I want to connect my node js to mongodb.
–> I am using mongodb version 6.5.0 (installed it as a service [complete install])
–> also I have installed mongosh (mongodb terminal)
–> I am able to access and modify my database using mongosh terminal (all commands are working in terminal)
–> In my project folder i first did "npm init", then "npm install node", then "npm install mongodb"(driver) & I am using nodeJS version 18.15.0
My code in "connect.js" file is as follows:
const {MongoClient} = require('mongodb');
const MONGO_URL = 'mongodb://localhost:27017';
const DB_NAME = 'tododb';
// ASYNC IIFE
(async () =>{
const client = await MongoClient.connect(MONGO_URL)
const tododb = client.db(DB_NAME)
console.log(tododb)
})()
and I got this error when I do "node connect.js" in vs code terminal
C:UsersGauravDesktopGaganDEVmongoBasicsnode_modulesmongodblibsdamtopology.js:277
const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);
^
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (C:UsersGauravDesktopGaganDEVmongoBasicsnode_modulesmongodblibsdamtopology.js:277:38)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 1431144101,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (C:UsersGauravDesktopGaganDEVmongoBasicsnode_modulesmongodblibcmapconnect.js:383:20)
at Socket.<anonymous> (C:UsersGauravDesktopGaganDEVmongoBasicsnode_modulesmongodblibcmapconnect.js:307:22)
at Object.onceWrapper (node:events:628:26)
at Socket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
cause: Error: connect ECONNREFUSED ::1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 27017
},
[Symbol(errorLabels)]: Set(1) { 'ResetPool' }
},
topologyVersion: null,
setName: null,
setVersion: null,
electionId: null,
logicalSessionTimeoutMinutes: null,
primary: null,
me: null,
'$clusterTime': null
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined,
[Symbol(errorLabels)]: Set(0) {}
}
Node.js v18.15.0
I want to use my nodeJS with mongoDB
2
Answers
You are trying to connect by ipV6, the line
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
shows that; as suggested by @Muhammad Fazeel , changeMONGO_URL
tomongodb://127.0.0.1:27017
This explicitly starts connection through ipV4
Go to file manager and 1st stop and then start MongoDB
Replace your db URL
mongodb://localhost:27017
tomongodb://127.0.0.1:27017