Nodejs和mongoose,迭代数据库来创建一个新的

时间:2018-02-20 16:27:27

标签: node.js mongoose

我有一个带有这个猫鼬模型的无序学生考试的集合:

var ExamSchema = new Schema({
  firstName: String,
  lastName: String,
  studentCode: String,
  examType: String,
  examDate: String,
  examResult: String
});

我需要根据此模型中的学生代码创建一个新的集合分组考试:

var StudentSchema = new Schema({
  firstName: String,
  lastName: String,
  studentCode: String,
  test: [{
    type: String,
    date: String,
    result: String
  }]
});

我尝试了这个功能,但它只是插入了所有学生而且从未找到学生代码。

function rebuildDb(err, done) {
 Exam.find({} , function(err, doc) {
  if(err) return err;
  doc.map( function(exam) {
        Student.findOne({ studentCode: exam.studentCode }, function(err, foundExam) {
         if(err) return callback(err);
         if(foundExam == null) {
           var student = new Student ({
             firstName: exam.firstName,
             lastName: exam.lastName,
             studentCode: exam.studentCode,
             test: [{
                type: exam.examType,
                date: exam.examDate,
                result: exam.examResult
              }],
           });
             student.save(function(err, newStudent) {
             if(err) return err;
             console.log("New student created: " + student.studentCode);
           });
         } else {
           Student.update({ studentCode: exam.studentCode }, { "$push": {
              test: {
                type: exam.examType,
                date: exam.examDate,
                result: exam.examResult
              }
            } }, function(err, newTest) {
           if(err) return err;
           console.log("New test for student: " + student.studentCode);
           });
         }
     });
  });
});
};

我认为这是同步/异步问题,但我不知道如何处理它。在这样的案例中,最佳做法是什么?

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个

const saveStudent = (exam) => {
    return new Promise((resolve, reject) => {
        const student = new Student({
            firstName: exam.firstName,
            lastName: exam.lastName,
            code: exam.code,
            test: [{
                type: exam.examType,
                date: exam.examDate,
                result: exam.examResult
            }],
        });
        student.save(function (err, newStudent) {
            if (err) return reject(err);
            console.log("New student created: " + student.code);
            resolve(newStudent);
        });
    });
}

const updateStudent = (exam) => {
    return new Promise((resolve, reject) => {
        Student.update({
            code: exam.code
        }, {
            "$push": {
                test: {
                    type: exam.examType,
                    date: exam.examDate,
                    result: exam.examResult
                }
            }
        }, function (err, newTest) {
            if (err) return reject(err);
            console.log("New test for student: " + student.code);
            resolve(newTest);
        });
    });
}

const findExam = async () => {
    const doc = await Exam.find({}).exec();
    doc.forEach(exam => {
        const foundExam = await Student.findOne({
            code: exam.code
        }).exec();
        if (foundExam == null) {
            await saveStudent(exam);
        } else {
            await updateStudent(exam);
        }
    });
}