I’m just learning mongodb for a JS class that uses Node JS, and I can’t get the first class example to work. I don’t know if it didn’t install correctly or if I missed a step. I use the terminal in Visual Studio Code to run the program. Here’s what I did:
I downloaded the MSI package for the community server and installed it with the default settings. It also installed Compass.
I did read on one website that I needed to add the C:\Program Files\MongoDB\Server\6.0\bin to Window’s Environmental Variables (Mongodb Installation on Windows, MacOS and linux)
I added C:\data\db
Using Command Prompt, I entered npm install mongodb
in Command Prompt, I entered mongod
Then in Visual Studio Code, where I have my program open, in the terminal command prompt, I change to the directory where my program is saved and run the following program by typing: node assignment4.js
PROGRAM:
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:3000/";
MongoClient.connect(url, function (err, db) {
if (err) {
throw err;
}
var dbo = db.db("testDB");
dbo.createCollection("customers", function(err, res) {
if (err) {
throw err;
}
console.log("customers Collection created");
db.close();
});
});
In the terminal, it pauses, and then spits this out:
C:\AIT618\node_modules\mongodb\lib\utils.js:365
throw error;
^
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:3000
at Timeout._onTimeout (C:\AIT618\node_modules\mongodb\lib\sdam\topology.js:291:38)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:3000' => ServerDescription {
address: 'localhost:3000',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 1639641464,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:3000
at connectionFailureError (C:\AIT618\node_modules\mongodb\lib\cmap\connect.js:387:20)
at Socket.<anonymous> (C:\AIT618\node_modules\mongodb\lib\cmap\connect.js:310:22)
at Object.onceWrapper (node:events:642:26)
at Socket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
cause: Error: connect ECONNREFUSED 127.0.0.1:3000
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3000
},
[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) {}
}
I tried moving the saved program to different folder on the C drive and rerunning the npm install mongodb and the mongod while in that directory, but same error.
I read somewhere it may have to do with my firewall, so I reset the Windows Defender firewall settings, but no luck. I also have Kaspersky Anti-virus on my machine. But I’m guessing it’s something really easy that I’m too new to this to see or understand.
I’m so frustrated, and would really appreciate any help!
Thank you.
