我努力将节点与 MongoDB 连接的原因
Reasons why am I struggling to connect node with MongoDB
我无法将 Node 连接到 MongoDB。
我分别测试了它们,它们工作正常。我还为数据库创建了一个路径。我还能缺少什么?
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
if(!err) {
console.log("We are connected");
}
});
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");
const db = client.db(dbName);
client.close();
});
尝试这样的事情:
/**
* Open connection with database.
*/
MongoClient.connect("exampleDb", { useNewUrlParser: true },
function(error, db) {
if (error) {
console.warn("MyDatabase : err1:" + error);
return;
}
const dbo = db.db(databaseName);
// Demo - Use it in usual way.
// dbo.collection("users").findOne({ "email": email }, function(err, result) {
// });
您可以在以下位置找到更多说明:
https://github.com/DefinitelyTyped/DefinitelyTyped/pull/27067
我无法将 Node 连接到 MongoDB。
我分别测试了它们,它们工作正常。我还为数据库创建了一个路径。我还能缺少什么?
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
if(!err) {
console.log("We are connected");
}
});
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");
const db = client.db(dbName);
client.close();
});
尝试这样的事情:
/**
* Open connection with database.
*/
MongoClient.connect("exampleDb", { useNewUrlParser: true },
function(error, db) {
if (error) {
console.warn("MyDatabase : err1:" + error);
return;
}
const dbo = db.db(databaseName);
// Demo - Use it in usual way.
// dbo.collection("users").findOne({ "email": email }, function(err, result) {
// });
您可以在以下位置找到更多说明:
https://github.com/DefinitelyTyped/DefinitelyTyped/pull/27067