1
こんにちは、私はノードに新しいです、そして、私はmongooseにデータを保存しようとしています。問題は、ユニット、ビルディング、セクションの3つのコレクションがあることです。 ビルのスキーマは次のとおりです。mongooseとasyncの滝モデルを使用してMongoDbにデータを保存する方法
var buildingsSchema=new Schema({
buildingname:String,
status:String
});
節のスキーマは次のとおりです。そこにある節で
var unitsSchema=new Schema({
unit_type:String,
unit_num:String,
unit_ac_num:Number,
buildings:{ type: Schema.Types.ObjectId, ref: 'buildings' },
sections:{ type: Schema.Types.ObjectId, ref: 'sections' },
shares:Number
});
:
var sectionsSchema=new Schema({
section_type:String,
buildings:{ type: Schema.Types.ObjectId, ref: 'buildings' },
status:String
});
単位のスキーマですビルのIDと単位でイドの両方のビル&節
のは、今私がでJSONにアップロードされたExcelファイルを変換するxlsxプラグインを使用しているがあります: -
var wb = XLSX.readFile("uploads/xls/" + req.file.filename);
data = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]], {header:1});
とマッピングするためにそれ私が
for (var index = 1; index < data.length - 1 ; index++) {
var ele= data[index];
// console.log(ele);
var d = { // this is an object which will hold data
record_id: ele[0],
residenceone_unit_id : ele[1],
official_unit_id: ele[2],
building: ele[3],
unit_type : ele[4],
section: ele[5],
shares: ele[6],
unit_style : ele[7]
}
// console.log(d);
var unt = new Units(); // to save units
unt = {
unit_type: d.unit_type,
unit_num: d.residenceone_unit_id,
unit_ac_num: d.official_unit_id,
buildings: '', // empty because need to put id of that particular building
sections: '', // empty because need to put id of that particular sections
shares:d.shares
}
async.waterfall([
function (callback) {
// find building first
Buildings.findOne({buildingname : ele.building})
.exec(function (err,doc) {
if (err) {
return callback(err);
}
// if no building then save one
else if (!doc) {
// console.log("Building is going to save")
var build = new Buildings();
build.buildingname = d.building;
build.status = "active";
build.save(function (err,dc) {
if (err) {
return callback(err);
}
else {
// assign id of building to unit
unt.buildings = dc._id ;
callback(null);
}
})
}
else {
// if building is there then no need to save assign id of it to units
// console.log("Building already exists;")
unt.buildings = doc._id ;
// execute second function
callback(null);
}
// callback(null);
})
},
function (callback) {
// same as building find section of that building first with building Id and section type
Sections.findOne({buildings : unt.buildings,section_type: d.section})
.exec(function (err,doc) {
if (err) {
return callback(err);
}
if (!doc) {
// if no section of that building is there then save one
// console.log("Section needs to be save")
var sect = new Sections();
sect.section_type = d.section;
sect.buildings = unt.buildings;
sect.status = "active";
sect.save(function (err,dc) {
if (err) {
return callback(err);
}
else {
// assign its id to unit
// console.log("Section is saved")
unt.sections = dc._id;
// execute third function
callback(null);
}
})
}
else {
// if there is section of that building id is available than assign id to units
// console.log("Section already exists");
unt.sections = doc._id;
// execute third function
callback(null);
}
})
},
function (callback) {
// there is no need to seaarch just need to save unit
// console.log("Units is going to save")
// console.log(unt);
unt.save(function (err, doc) {
if (err) {
} else if (doc){
// console.log("Unit Saved");
// console.log(doc);
}
})
}
])
}
}
を
非同期滝モデルを使用していますJSONオブジェクトに変換し、保存0 は動作しますが、毎回mongodbのデータを検索するのではなく、毎回保存します。 重複は、mongodbのユニットを保存するための他の方法があれば、大きな問題になります。最初
。 Buildings.findOne({buildingname:ele.building})ele.buildingは存在しませんあなたは未定義を検索しています。d.buildingを使用する必要があります –
兄さんそれはちょうど問題です。データが繰り返し挿入され、私の実際のデータが入ります配列 –
'for(var index = 1; ...)'の代わりに、 '' async.eachSeries() '](http://caolan.github.io/async/docs.html#eachSeries)を使用してください。 (または、おそらく 'async.eachLimit')。 – robertklep