我正在以1000的批量插入数据。
我有这个功能来处理插入:
function insertInBatches(i, dataParsed, RecordModel, callback){
var numberOfBatches = (Math.floor(dataParsed.length/1000)) + 1;
console.log("Inserting batch #"+i +" out of: " +numberOfBatches);
var batchedData = dataParsed.slice(i*1000, (i+1)*1000);
console.log("Batched data: "+JSON.stringify(batchedData));
RecordModel.insertMany(batchedData)
.then(function(mongooseDocuments) {
console.log(" - Insertion was successful.");
})
.catch(function(err) {
callback("Error while inserting data to DB: "+err);
return;
})
.done(function() {
if (i==numberOfBatches-1){
// Last iteration, all inserted successfully
console.log("Insertion of: "+numberOfBatches+" batches was successful.");
callback(null);
return;
}
// Recursive calling to insert next batch
console.log("Value of i: "+i);
insertInBatches(++i, dataParsed, RecordModel);
return;
});
}
输出日志如下所示:
Model created. Inserting in batches.
Inserting batch #0 out of: 1
Batched data: [{"SCHID":"config","Priority":3,"DateCreated":"2016-08-04T13:22:04.443Z","Status":"queued","Json":{"EmailAddress":"oto@increase.dk","FirstName":"Ondrej","Age":"22"}},{"SCHID":"config","Priority":3,"DateCreated":"2016-08-04T13:22:04.443Z","Status":"queued","Json":{"EmailAddress":"lfr@increase.dk","FirstName":"Lenka","Age":"23"}}]
- Insertion was successful.
Insertion of: 1 batches was successful.
但是当我从bash执行db.MainRecord.find()
时,我看不到任何结果。
我的RecordModel
是:{SCHID: String, Priority: Number, DateCreated: Date, Status: String, Json: mongoose.Schema.Types.Mixed};
db的路径:
generalConfig.dbName = "test";
generalConfig.recordsCollectionName = "MainRecords";
generalConfig.schemaDefinition = {SCHID: String, Priority: Number, DateCreated: Date, Status: String, Json: mongoose.Schema.Types.Mixed};
generalConfig.RecordSchema = new mongoose.Schema(generalConfig.schemaDefinition);
generalConfig.RecordModel = mongoose.model(generalConfig.recordsCollectionName, generalConfig.RecordSchema, generalConfig.recordsCollectionName);
修改
我只需要重新启动服务器上的mongodb
。不知道为什么。
EDIT2:
问题是我的愚蠢,我在错误的集合上找到了查找:MainRecord
而不是MainRecords
......