-1

```const timeoutError = new error_1.MongoServerSelectionError(Server selection timed out after ${serverSelectionTimeoutMS} ms, this.description); .MongoServerSelectionError: connect ECONNREFUSED ::1:27017

const {MongoClient}=require('mongodb');

const url='mongodb://localhost:27017/';

const client= new MongoClient(url);

const dataBase= 'nodejs';

async function getdata(){
    
    let result= await client.connect(); 
     console.log('connect to server')
    let db= result.db(dataBase)
    console.log('2')
    let collection = db.collection('node');
    let response= await collection.find({}).toArray();
    console.log(response)
}

getdata();
1
  • Is your mongo server up and running? Doesn't seem like it ... Commented Nov 29, 2022 at 12:21

2 Answers 2

1

Change uri to

const url='mongodb://0.0.0.0:27017/';
Sign up to request clarification or add additional context in comments.

4 Comments

but can you explain why we add "mongodb://0.0.0.0:27017/" instead of localhost
0.0.0.0, in this context, means "all IP addresses on the local machine" (in fact probably, "all IPv4 addresses on the local machine").
ohh so the local machine ip is not fixed. so is any other option instead of this?
Please have a look at this it explains in detail serverfault.com/questions/78048/…
1

This happened probably because the MongoDB service isn't started.

Follow the below steps to start it:

  1. Go to Control Panel and click on Administrative Tools. (in Windows the path will be: Control Panel\System and Security\Administrative Tools)
  2. Double click on Services. A new window opens up.
  3. Search MongoDB.exe. Right click on it and select Start.

The server will start. Now execute npm start again and the code might work this time.

The answer from a similar question (I can't comment so I post as an answer)

2 Comments

sir actiually i follow this process but i not found any file mongodb.exe file in services
actually i'm using mongoDB 6.0 version. i'm check the version of mongoDB in my pc and not get any version just mongo is not internal and external command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.