如何在mongodb

时间:2016-09-07 07:15:13

标签: javascript node.js mongodb mongoose

使用此代码,我不确定在推送每个问题之后是否使用examBoard.save()'进入' examBoard'或者在推完最后一个问题之后进入' examBoard'就像我在代码中所说的那样。

 examBoard.create({
   name: "AQA",
   modules: [{
     name: "c1",
     topics: ["c1a", "c1b", "c1c"]
   }, {
     name: "c2",
     topics: ["c2a", "c2b", "c2c"]
   }, {
     name: "c3",
     topics: ["c3a", "c3b", "c3c"]
   }],
   function(err, examBoard) {
     if (err) {
       console.log("Failed to make new exam board\n" + err);
     } else {
       //=================================
       //adding question to AQA exam board
       //=================================
       question.create({
         text: "question 1a",
         parts: [
           ["1a", "1b", "1c"],
           ["2a", "2b", "2c"],
           ["3a", "3b", "3c"]
         ]
       }, function(err, question) {
         if (err) {
           console.log("Failed to make new question\n" + err);
         } else {
           examBoard.modules[0].topics[0].push(question);
           console.log("New question made\n" + question);
         }
       });
       question.create({
         text: "question 1b",
         parts: [
           ["1a", "1b", "1c"],
           ["2a", "2b", "2c"],
           ["3a", "3b", "3c"]
         ]
       }, function(err, question) {
         if (err) {
           console.log("Failed to make new question\n" + err);
         } else {
           examBoard.modules[0].topics[1].push(question);
           console.log("New question made\n" + question);
         }
       });
       question.create({
         text: "question 1c",
         parts: [
           ["1a", "1b", "1c"],
           ["2a", "2b", "2c"],
           ["3a", "3b", "3c"]
         ]
       }, function(err, question) {
         if (err) {
           console.log("Failed to make new question\n" + err);
         } else {
           examBoard.modules[0].topics[2].push(question);
           console.log("New question made\n" + question);
         }
       });
       question.create({
         text: "question 2a",
         parts: [
           ["1a", "1b", "1c"],
           ["2a", "2b", "2c"],
           ["3a", "3b", "3c"]
         ]
       }, function(err, question) {
         if (err) {
           console.log("Failed to make new question\n" + err);
         } else {
           examBoard.modules[1].topics[0].push(question);
           console.log("New question made\n" + question);
         }
       });
       question.create({
         text: "question 2b",
         parts: [
           ["1a", "1b", "1c"],
           ["2a", "2b", "2c"],
           ["3a", "3b", "3c"]
         ]
       }, function(err, question) {
         if (err) {
           console.log("Failed to make new question\n" + err);
         } else {
           examBoard.modules[1].topics[1].push(question);
           console.log("New question made\n" + question);
         }
       });
       question.create({
         text: "question 2c",
         parts: [
           ["1a", "1b", "1c"],
           ["2a", "2b", "2c"],
           ["3a", "3b", "3c"]
         ]
       }, function(err, question) {
         if (err) {
           console.log("Failed to make new question\n" + err);
         } else {
           examBoard.modules[1].topics[2].push(question);
           console.log("New question made\n" + question);
         }
       });
       question.create({
         text: "question 3a",
         parts: [
           ["1a", "1b", "1c"],
           ["2a", "2b", "2c"],
           ["3a", "3b", "3c"]
         ]
       }, function(err, question) {
         if (err) {
           console.log("Failed to make new question\n" + err);
         } else {
           examBoard.modules[2].topics[0].push(question);
           console.log("New question made\n" + question);
         }
       });
       question.create({
         text: "question 3b",
         parts: [
           ["1a", "1b", "1c"],
           ["2a", "2b", "2c"],
           ["3a", "3b", "3c"]
         ]
       }, function(err, question) {
         if (err) {
           console.log("Failed to make new question\n" + err);
         } else {
           examBoard.modules[2].topics[1].push(question);
           console.log("New question made\n" + question);
         }
       });
       question.create({
         text: "question 3c",
         parts: [
           ["1a", "1b", "1c"],
           ["2a", "2b", "2c"],
           ["3a", "3b", "3c"]
         ]
       }, function(err, question) {
         if (err) {
           console.log("Failed to make new question\n" + err);
         } else {
           examBoard.modules[2].topics[2].push(question);
           examBoard.save();
           console.log("New question made\n" + question);
         }
       });
       console.log("New exam board made\n" + examBoard);

     }
   }
 });

1 个答案:

答案 0 :(得分:0)

你这样做的方式很好。

因为如果任何问题都失败并且两者之间发生任何错误,您的完整交易将失败,并且您不会在数据库中保存一半问题。

在其他情况下(在每个问题之后保存)可能会结束保存的一半问题,并且在发生错误时不会休息。 当您向客户回复交易失败时,客户将再次发布数据,它将创建重复的问题/ examBoard(那些在错误之前被保存的人)。