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:
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 .
当我将“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.