简体   繁体   中英

Can't connect to a MongoDB using Node.js

Can't connect to a MongoDB using Node.js.

This is my code (mdb.js):

var MongoClient = require('mongodb').MongoClient, format = require('util').format;

MongoClient.connect('mongodb://localhost:3000', function(err,db){

    if(err){
         throw err;
    } else {
         console.log("Connected");
    }
    db.close();
});

node mdb.js prints MongoError:

enter image description here

Your error explaining itself. Mongo cant find default db path B:/data/db .

Create this folder or on start up choose another DB directory

mongod --dbpath yourPath(C:\myDb)

Also default mongo port is 27017 so you need to change connection string like this "mongodb://@localhost:27017/dbYouWant"

Hope this helps.

First make sure your MongoDB server is running by running mongod in the directory that MongoDB is installed.

Then see what port it is running on ( usually it's 27017 ) . Then update your URL in your code and you're good to go .

这是我尝试启动 Mongo 的内容:

I've created those folders, now it starts and listens, but with these messages. I'm new to MongoDB, so I'm not sure if it is normal:

在此处输入图像描述

当我将“localhost”更改为 127.0.0.1 时,它对我有用

Use this URL mongodb://0.0.0.0:3000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM