My MongoDB server is running locally, i can execute my commands on shell also but when i try to connect MongoDB with Node.js it gives me error

X:\BackEnd Web Development\fruits\node_modules\mongoose\lib\connection.js:755
err = new ServerSelectionError();
^

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at _handleConnectionErrors (X:\BackEnd Web Development\fruits\node_modules\mongoose\lib\connection.js:755:11)
at NativeConnection.openUri (X:\BackEnd Web Development\fruits\node_modules\mongoose\lib\connection.js:730:11) {
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: 181899026,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (X:\BackEnd Web Development\fruits\node_modules\mongodb\lib\cmap\connect.js:383:20)
at Socket. (X:\BackEnd Web Development\fruits\node_modules\mongodb\lib\cmap\connect.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
}

Node.js v18.15.0

Hello @Rahul_pal

Can you please provide the Node.JS script you’re using?
Are you using the MongoDB Node.JS Driver? I see you’re using Mongoose via the directory.
What type of app is this? What is this running on?

Use 127.0.0.1 rather than localhost.

The following

indicates that your localhost resolve to the IPv6 version.

1 Like
const mongoose = require('Mongoose');
mongoose.connect("MongoDB://localhost:<PortNumberHereDoubleCheckPort>/<DatabaseName>", {useNewUrlParser: true});
const <nameOfDbschemahere> = new mongoose.schema({
  name: String,
  rating: String,
  quantity: Number,
  someothervalue: String,
  somevalue2: String,
});

const Fruit<Assuming as you call it FruitsDB> = mongoose.model("nameOfCollection" , <nameOfSchemeHere>);

const fruit = new Fruit<Because FruitsDB calling documents Fruit for this>({
  name: "Watermelon",
  rating: 10,
  quantity: 50,
  someothervalue: "Pirates love them",
  somevalue2: "They are big",
});
fruit.save();

In MongoDB when logged in via terminal:
show dbs
Select the DB in this case whatever you named the database by typing: use databasename
show collections
You should then see the fruits collection.
db.fruits.find will then pull up all documents in fruit.

tried it but it gets stuck every time

@Brock Just 2 line script requiring mongoose and connecting it.
I just started learning MongoDb and it’s my first MongoDb project trying to connect with Node.js

It should work with 127.0.0.1
What error are you getting?timeout or someother?

@Ramachandra_Tummala I waited for 5 minutes and it still did not give me nothing it gets freeze

Is your code complete?
After useNewUrlParser: true there is a comma
Do you have any other parameter before closing the brackets?
May be waiting for more inputs?

That’s bizarre, it’s working fine on my local environment, there shouldn’t be other parameters needed. The comma is because it’s continuing with the parser.

That said, there’s nothing returning because nothing is there @Rahul_pal. You need to build the schema, the DB, etc.

@Rahul_pal if it’s been done correctly, you can do the following to check the work:

In MongoDB when logged in via terminal:
show dbs
Select the DB in this case whatever you named the database by typing: use databasename
show collections
You should then see the fruits collection.
db.fruits.find will then pull up all documents in fruit.

What the freeze is indicating is there is nothing to CD into, you need to not just “connect” it, but you need to “build” it.

Hey, also make sure that the port is correct in the configs, and use ls to see if you’re in the right directory for that project, too.

It worked for me, Thanks

1 Like