我的代码是:
var MongoClient = require("mongodb").MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url,function(err,db){
if(err) throw err;
var dbo = db.db("school");
dbo.createCollection("students",function(err,res){
console.log("collection created!!!");
});
var myObjList=[
{
name:"rohit",
age:15,
gpa:4.9
},
{
name:"rahul",
age:17,
gpa:4.7
}
]
dbo.collection("students").insertMany(myObjList,function(err,res){
if(err) throw err;
console.log(res);
db.close();
})
});
我想将数据插入 MongoDB 存储,但未能将数据插入 MongoDB 存储。此代码未建立 MongoDB 连接。当我尝试运行 Node js 代码但在控制台中看到此错误时:
const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);
^
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (C:\Users\Administrator\node_modules\mongodb\lib\sdam\topology.js:278:38)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7) {
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: 2471400,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (C:\Users\Administrator\node_modules\mongodb\lib\cmap\connect.js:379:20)
at Socket.<anonymous> (C:\Users\Administrator\node_modules\mongodb\lib\cmap\connect.js:285: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) {
[Symbol(errorLabels)]: Set(1) { 'ResetPool' },
[cause]: Error: connect ECONNREFUSED ::1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 27017
}
},
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,
[Symbol(errorLabels)]: Set(0) {},
[cause]: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (C:\Users\Administrator\node_modules\mongodb\lib\cmap\connect.js:379:20)
at Socket.<anonymous> (C:\Users\Administrator\node_modules\mongodb\lib\cmap\connect.js:285: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) {
[Symbol(errorLabels)]: Set(1) { 'ResetPool' },
[cause]: Error: connect ECONNREFUSED ::1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 27017
}
}
}
请帮我解决这个问题。
MongoDb URL应该更改为
mongodb://0.0.0.0:27017
,我也借此机会编辑了代码。
var MongoClient = require("mongodb").MongoClient;
var url = "mongodb://0.0.0.0:27017/";
MongoClient.connect(url,function(err,db){
if(err) {
console.log("error connecting" + err);
}
console.log("connection successful");
var dbo = db.db("school");
dbo.createCollection("students",function(err,res){
if(err){
console.log("error creating collection: "+err);
db.close();
return;
}
console.log("collection created!!!");
});
var myObjList=[
{
name:"rohit",
age:15,
gpa:4.9
},
{
name:"rahul",
age:17,
gpa:4.7
}
]
dbo.collection("students").insertMany(myObjList,function(err,res){
if(err){
console.log("error inserting " + myObjList +": "+err);
db.close();
return;
}
console.log(res);
db.close();
})
});
由于您在终端得到空输出,我创建了一些控制台日志,以便您在遇到错误时收到通知。
快乐编码!