Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何使用mongoose和异步瀑布模型在MongoDb中存储数据_Json_Node.js_Mongodb_Asynchronous_Mongoose - Fatal编程技术网

Json 如何使用mongoose和异步瀑布模型在MongoDb中存储数据

Json 如何使用mongoose和异步瀑布模型在MongoDb中存储数据,json,node.js,mongodb,asynchronous,mongoose,Json,Node.js,Mongodb,Asynchronous,Mongoose,您好,我是node的新手,正在尝试用mongoose保存数据。问题是有3个集合单元,建筑和部分。建筑的模式为: var buildingsSchema=new Schema({ buildingname:String, status:String }); 部分的架构是: var sectionsSchema=new Schema({ section_type:String, buildings:{ type: Schema.Types.Objec

您好,我是node的新手,正在尝试用mongoose保存数据。问题是有3个集合单元,建筑和部分。建筑的模式为:

var buildingsSchema=new Schema({
    buildingname:String,
    status:String
});
部分的架构是:

var sectionsSchema=new Schema({
        section_type:String,
        buildings:{ type: Schema.Types.ObjectId, ref: 'buildings' },
        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
 });
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
});
区域中有建筑的Id,在单元中有建筑和区域的Id

现在,我使用插件将上传的excel文件转换为json,方法是:-

 var wb = XLSX.readFile("uploads/xls/" + req.file.filename);
 data = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]],   {header:1});
为了将它映射到json对象并保存,我使用了异步瀑布模型

        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);

                    }

                })
            }
         ])
    }
}
for(var index=1;index

它可以工作,但每次都会保存,而不是在mongodb中搜索数据复制是一个主要的问题,如果有任何其他方法来保存mongodb中的单元将对我有很大帮助。

首先,我保存了建筑和部分

async.every(uniqueSection,function (uS,callback) {
      if (uS != undefined) {
          console.log(uS);
            Buildings.findOne({buildingname:uS.building}, function (err,doc) {
                if (doc) {
                    // console.log(doc);
                    Sections.findOne({buildings:doc._id,section_type:uS.section},function (er,sd) {
                        if (sd) {
                            // then dont save
                            console.log(sd);
                        }
                        if (er) {
                            console.log(er);
                        }
                        if (!sd) {
                            // then save
                            var sect = new Sections();
                            sect.buildings = doc._id;
                            sect.section_type = uS.section;
                            sect.status = 'active';
                            sect.save(function (err,st) {
                                if(err) console.log(err);
                                console.log(st);
                            })
                        }
                    })  
                } 
                if (!doc) {
                 if (uS.building != undefined) {
                    var building = new Buildings();
                    building.buildingname = uS.building;
                    building.status = "active";
                    building.save(function (er,dc) {
                        if (dc) {
                            // console.log(dc);
                            Sections.findOne({buildings:dc._id,section_type:uS.section},function (er,sd) {
                                if (sd) {
                                    // then dont save
                                    console.log(sd);
                                }
                                if (er) {
                                    console.log(er);
                                }
                                if (!sd) {
                                    // then save
                                    var sect = new Sections();
                                    sect.buildings = dc._id;
                                    sect.section_type = uS.section;
                                    sect.status = 'active';
                                    sect.save(function (err,st) {
                                        if(err) console.log(err);
                                        console.log(st);
                                    })
                                }
                            })  
                        } 
                        if (er) {
                            console.log(er);
                        }
                    })
                  }
                }
                if (err) {
                    console.log(err);
                }
              })
            }
        })
然后我通过以下方式保存了单位

   async.waterfall([
                function(callback) {
                    Buildings.findOne({buildingname:d.building}, function (err,doc) {
                        if (doc) {
                            buildingId = doc._id;
                            callback(null, doc);
                        } 
                        if (err) {
                            console.log(err);
                        }
                      })

                },
                function(doc,callback) {
                    Sections.findOne({buildings: buildingId,section_type:d.section},function (er,sd) {
                        if (sd) {
                            sectionId = sd._id;
                           callback(null,doc,sd);  
                        }
                        if (er) {
                            console.log(er);
                        }

                    })  
                },
                function (bld,st,callback) {
                var s = d.shares.replace(",","");
                var unit = {
                    unit_type: d.unit_type,
                    unit_num: d.residenceone_unit_id,
                    unit_ac_num: d.official_unit_id,
                    buildings: bld._id, 
                    sections: st._id,
                    shares: s
                 }
                 Units.findOne(unit,function (err,unt) {
                     if (err) {
                         console.log(err);
                     } 
                     if(unt) {
                         console.log(unt)
                     }
                     if (!unt) {
                        var units = new Units();
                        units.unit_type = d.unit_type;
                        units.unit_num = d.residenceone_unit_id;
                        units.unit_ac_num = d.official_unit_id;
                        units.buildings = bld._id; 
                        units.sections = st._id;
                        units.shares = s;

                        units.save(function (er,doc) {
                            if (er) console.log(er);
                            console.log(doc);
                        }) 
                     }
                 })
                }
            ], function(err) {
                console.log(err)
            });

首先,我保存了建筑和部分:

async.every(uniqueSection,function (uS,callback) {
      if (uS != undefined) {
          console.log(uS);
            Buildings.findOne({buildingname:uS.building}, function (err,doc) {
                if (doc) {
                    // console.log(doc);
                    Sections.findOne({buildings:doc._id,section_type:uS.section},function (er,sd) {
                        if (sd) {
                            // then dont save
                            console.log(sd);
                        }
                        if (er) {
                            console.log(er);
                        }
                        if (!sd) {
                            // then save
                            var sect = new Sections();
                            sect.buildings = doc._id;
                            sect.section_type = uS.section;
                            sect.status = 'active';
                            sect.save(function (err,st) {
                                if(err) console.log(err);
                                console.log(st);
                            })
                        }
                    })  
                } 
                if (!doc) {
                 if (uS.building != undefined) {
                    var building = new Buildings();
                    building.buildingname = uS.building;
                    building.status = "active";
                    building.save(function (er,dc) {
                        if (dc) {
                            // console.log(dc);
                            Sections.findOne({buildings:dc._id,section_type:uS.section},function (er,sd) {
                                if (sd) {
                                    // then dont save
                                    console.log(sd);
                                }
                                if (er) {
                                    console.log(er);
                                }
                                if (!sd) {
                                    // then save
                                    var sect = new Sections();
                                    sect.buildings = dc._id;
                                    sect.section_type = uS.section;
                                    sect.status = 'active';
                                    sect.save(function (err,st) {
                                        if(err) console.log(err);
                                        console.log(st);
                                    })
                                }
                            })  
                        } 
                        if (er) {
                            console.log(er);
                        }
                    })
                  }
                }
                if (err) {
                    console.log(err);
                }
              })
            }
        })
然后我通过以下方式保存了单位

   async.waterfall([
                function(callback) {
                    Buildings.findOne({buildingname:d.building}, function (err,doc) {
                        if (doc) {
                            buildingId = doc._id;
                            callback(null, doc);
                        } 
                        if (err) {
                            console.log(err);
                        }
                      })

                },
                function(doc,callback) {
                    Sections.findOne({buildings: buildingId,section_type:d.section},function (er,sd) {
                        if (sd) {
                            sectionId = sd._id;
                           callback(null,doc,sd);  
                        }
                        if (er) {
                            console.log(er);
                        }

                    })  
                },
                function (bld,st,callback) {
                var s = d.shares.replace(",","");
                var unit = {
                    unit_type: d.unit_type,
                    unit_num: d.residenceone_unit_id,
                    unit_ac_num: d.official_unit_id,
                    buildings: bld._id, 
                    sections: st._id,
                    shares: s
                 }
                 Units.findOne(unit,function (err,unt) {
                     if (err) {
                         console.log(err);
                     } 
                     if(unt) {
                         console.log(unt)
                     }
                     if (!unt) {
                        var units = new Units();
                        units.unit_type = d.unit_type;
                        units.unit_num = d.residenceone_unit_id;
                        units.unit_ac_num = d.official_unit_id;
                        units.buildings = bld._id; 
                        units.sections = st._id;
                        units.shares = s;

                        units.save(function (er,doc) {
                            if (er) console.log(er);
                            console.log(doc);
                        }) 
                     }
                 })
                }
            ], function(err) {
                console.log(err)
            });

嗨,我想你的问题是这个。Buildings.findOne({buildingname:ele.building})ele.building不存在您正在搜索未定义的您需要使用d.buildingbrother它可以工作