Answer the question
In order to leave comments, you need to log in
How to initialize Mongodb with data?
I set the initial data for a DB a trace. way:
const ModTpl = require('../models/mod_tpl');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/testdb', { useMongoClient: true });
//Здесь набор самих данных
const data = [ ... ];
let done = 0;
for (let i = 0; i < data.length; i++) {
data[i].save(function(err, result) {
// if (err) {
// console.log(i, err);
// mongoose.disconnect();
// return;
// }
done++;
if (done === data.length) {
exit();
}
});
}
function exit() {
mongoose.disconnect();
}
Answer the question
In order to leave comments, you need to log in
I would use async lib something like this:
const async = reuire('async');
let queue = async.queue(function(task, callback) {
task.save(callback);
}, 10);
queue.drain = function() {
exit();
};
for (let i = 0; i < data.length; i++) {
queue.push(data[i]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question