gpt4 book ai didi

javascript - 使用 node.js 将多个文档插入到 Mongodb

转载 作者:行者123 更新时间:2023-11-30 10:12:41 25 4
gpt4 key购买 nike

我正在尝试使用 node.js 将多个文档插入到我的数据库中,但我遇到了一个错误:MongoError:连接被应用程序关闭有没有并行插入多个文档的选项?

这是我的代码:

var MongoClient = require('mongodb').MongoClient;



var dbName = "tst1";
var port = "27017";
var requiredCollection = "stocks"
var host = "localhost";

// open the connection the DB server

MongoClient.connect("mongodb://" + host + ":" + port + "/" + dbName, function (error, db){

console.log("Connection is opened to : " + "mongodb://" + host + ":" + port + "/" + dbName);

if(error) throw error;

var ibm = {'_id' : 1, 'value' : 1, 'ticker' : 'IBM'};

db.collection(requiredCollection).insert(ibm, function(error, inserted) {
if(error) {
console.error(error);

}
else {
console.log("Successfully inserted: " , inserted );
}

}); // end of insert


var apple = {'_id' : 2, 'vlue' : 1, 'ticker' : 'AAPL'};

db.collection(requiredCollection).insert(apple, function(error, inserted) {
if(error) {
console.error(error);

}
else {
console.log("Successfully inserted: " , inserted );
}

}); // end of insert





var intel = {'_id' : 3, 'value' : 1, 'ticker' : 'INTC'};

db.collection(requiredCollection).insert(intel, function(error, inserted) {
if(error) {
console.error(error);

}
else {
console.log("Successfully inserted: " , inserted );
}

}); // end of insert


var f5 = {'_id' : 4, 'value' : 1, 'ticker' : 'FFIV'};

db.collection(requiredCollection).insert(f5, function(error, inserted) {
if(error) {
console.error(error);

}
else {
console.log("Successfully inserted: " , inserted );
}

}); // end of insert




var arris = {'_id' : 5, 'value' : 1, 'ticker' : 'ARRS'};

db.collection(requiredCollection).insert(arris, function(error, inserted) {
if(error) {
console.error(error);

}
else {
console.log("Successfully inserted: " , inserted );
}

}); // end of insert

db.close();





}); // Connection to the DB

最佳答案

在 MongoDB 3.2 及以上版本中,您可以使用 db.collection.insertMany() 将多个文档保存到一个集合中。 ( see documentation )

您的代码可以简化为:

var MongoClient = require('mongodb').MongoClient;

var dbName = "tst1";
var port = "27017";
var requiredCollection = "stocks"
var host = "localhost";

// open the connection the DB server

MongoClient.connect("mongodb://" + host + ":" + port + "/" + dbName, function (error, db){

console.log("Connection is opened to : " + "mongodb://" + host + ":" + port + "/" + dbName);

if(error) throw error;

var docs = [{ _id: 1, value: 1, ticker: 'IBM' },
{ _id: 2, value: 1, ticker: 'AAPL' },
{ _id: 3, value: 1, ticker: 'INTC' },
{ _id: 4, value: 1, ticker: 'FFIV' },
{ _id: 5, value: 1, ticker: 'ARRS' }];

db.collection(requiredCollection).insertMany(docs, function(error, inserted) {
if(error) {
console.error(error);
}
else {
console.log("Successfully inserted: " , inserted );
}

}); // end of insert

db.close();

}); // Connection to the DB

关于javascript - 使用 node.js 将多个文档插入到 Mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25604276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com