[英]Having trouble inserting documents in mongoDB using mongoose and node.js
[英]Inserting multiple documents to Mongodb using node.js
我正在嘗試使用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()
將多個文檔保存到一個集合中。 ( 請參閱文檔 )
您的代碼可以簡化為:
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
“異步”庫在這里為您提供了幫助,因為您需要了解異步代碼中的“回調”,而幫助您的主要事情是通過消除對代碼中的每個“下一個”調用“縮進”的需要來進行“代碼爬行”。
實際上,對於合理數量的操作,您可以“並行”執行這些操作,而不是“串行”執行。 我們只需要“等待”完成每個任務,這就是“回調”的目的。 操作完成后,它將“回調”以調用“下一個動作”:
var MongoClient = require('mongodb').MongoClient,
async = require('async');
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;
async.parallel(
[
function(callback) {
var ibm = {'_id' : 1, 'value' : 1, 'ticker' : 'IBM'};
db.collection(requiredCollection).insert(ibm, function(error, inserted) {
if(error) {
console.error(error);
callback(error);
} else {
console.log("Successfully inserted: " , inserted );
callback();
}
}); // end of insert
},
function(callback) {
var apple = {'_id' : 2, 'vlue' : 1, 'ticker' : 'AAPL'};
db.collection(requiredCollection).insert(apple, function(error, inserted) {
if(error) {
console.error(error);
callback(error);
} else {
console.log("Successfully inserted: " , inserted );
callback();
}
}); // end of insert
},
function(callback) {
var intel = {'_id' : 3, 'value' : 1, 'ticker' : 'INTC'};
db.collection(requiredCollection).insert(intel, function(error, inserted) {
if(error) {
console.error(error)
callback(error);
} else {
console.log("Successfully inserted: " , inserted );
callback();
}
}); // end of insert
},
function(callback) {
var f5 = {'_id' : 4, 'value' : 1, 'ticker' : 'FFIV'};
db.collection(requiredCollection).insert(f5, function(error, inserted) {
if(error) {
console.error(error);
callback(error);
} else {
console.log("Successfully inserted: " , inserted );
callback();
}
}); // end of insert
},
function(callback) {
var arris = {'_id' : 5, 'value' : 1, 'ticker' : 'ARRS'};
db.collection(requiredCollection).insert(arris, function(error, inserted) {
if(error) {
console.error(error)
callback(error);
} else {
console.log("Successfully inserted: " , inserted );
}
}); // end of insert
},
],
function(err) {
// called when everything is done
db.close();
}
);
}); // Connection to the DB
現在,每個操作都等待從其自身的“回調”上下文中調用其“回調”,以及等待“流控制”,直到所有操作完成之后才最終在所有操作結束時“關閉”連接。
但是如前所述,除非這是一個“一次性”腳本,否則您基本上不會在數據庫連接上調用.close()
,並且只會打開一次 。
您可以使用mongo批量插入https://docs.mongodb.com/manual/reference/method/Bulk.insert/
var bulk = db.items.initializeUnorderedBulkOp();
bulk.insert( { item: "abc123", defaultQty: 100, status: "A", points: 100 } );
bulk.insert( { item: "ijk123", defaultQty: 200, status: "A", points: 200 } );
bulk.insert( { item: "mop123", defaultQty: 0, status: "P", points: 0 } );
bulk.execute();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.