2017-06-15 104 views
0

我正在構建一個簡單的網絡應用程序,公司向其員工發送一個問題,請求反饋。仍在學習mongodb。一直玩弄它一週&我在論壇上慢慢獲得了一些有用的幫助,但只是現在我意識到我一直在使用有缺陷的思維過程來設計架構。我最初使用用戶的響應作爲UserSchema一個領域,但現在我已刪除了它(如註釋掉這裏),因爲我意識到這不是用戶的財產而是不斷變化的變量(是/否/空) 。我現在必須創建一個單獨的AnswersSchema(我被告知我需要一個,但我頑固地反對它 - 當我開始項目時沒有意識)我現在已經做了(糾正我,如果它被錯誤地寫/思考出)。我現在的問題是如何修改我的查詢在API的所有三個實體一起在save操作在routerpost鏈接? 請注意save此處顯示的操作代碼可行,但存在缺陷,因爲它適用於用戶將響應作爲其屬性之一。所以現在只有用戶的名字出現在角度前端,我刪除了UserSchema的迴應,這是有道理的。快速:mongodb貓鼬鏈接實體

var QuestionSchema = Schema({ 
    id   : ObjectId, 
    title  : String, 
    employees : [{ type: ObjectId, ref: 'User'}] 
}); 

var UserSchema = Schema({ 
    username : String, 
    //response : String, 
    questions : [{ type: ObjectId, ref: 'Question'}] 
}); 

//new schema/collection I've had to create 
var AnswerSchema = Schema({ 
    response : {type :String, default:null}, 
    question : { type: ObjectId, ref: 'Question'}, 
    employees : [{ type: ObjectId, ref: 'User'}], 
}) 

module.exports = mongoose.model('Question', QuestionSchema); 
module.exports = mongoose.model('User', UserSchema); 
module.exports = mongoose.model('Answer', AnswersSchema); 

api.js

Question.findOne({ title: 'Should we buy a coffee machine?'}).exec(function(err, question) { 
      //example data 
      var user = new User([{ 
      "username": "lindelof", 
      "response": "yes", 
      },{ 
      "username": "bailly", 
      "response": "no", 
      },{ 
      "username": "suzan", 
      "response": "yes", 
      }]); 

      question.employees = [user1._id]; 
      user.questions = [question._id]; 

      question.save(function(err) { 
       if (err) throw err; 
       console.log(question); 
       user1.save(function(err) { 
        if (err) throw err; 
       }); 
      }); 

     }); 
     console.log('entry saved >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'); 
    } 

UPDATE enter image description here

回答

1

您加入AnswerSchema,因爲它是一個多對多的關係做了正確的事情。一個問題可以由許多用戶(員工)解答。用戶可以回答很多問題。因此,作爲兩者之間的聯合集合來回答是很好的。

enter image description here

考慮到這層關係,你需要改變你的架構一點:

var QuestionSchema = Schema({ 
    id   : ObjectId, 
    title  : String, 
    //employees : [{ type: ObjectId, ref: 'User'}] 
}); 

var UserSchema = Schema({ 
    username : String, 
    //response : String, 
    //questions : [{ type: ObjectId, ref: 'Question'}] 
}); 

var AnswerSchema = Schema({ 
    response : {type :String, default:null}, 
    question : { type: ObjectId, ref: 'Question'}, 
    employee : { type: ObjectId, ref: 'User'}, //a single employee 
}); 

現在,要知道,如果某用戶已經回答的問題,只是搜索Answer與他和問題的ID:

Answer.findOne({ 
     question: questionId, 
     employee: userId 
    }) 
    .exec(function(err, answer) { 
     if (err) { 

     } else if (!answer) { 
      //the employee has not answered this question yet 
     } else { 
      //answered 
     } 
    }); 

最後,您的提交答案API應該期望包含questionId和userI的正文d(如果已登錄,則可以從會話或令牌獲取userId)。此路線更新現有的答案,否則創建它(爲創建使用create功能)

router.post('/', function(req, res) { 
      //req.body = {question: "594315b47ab6ecc30d5184f7", employee: "594315d82ee110d10d407f93", response: "yes"} 
      Answer.findOneAndUpdate({ 
        question: req.body.question, 
        employee: req.body.user 
       }, 
       req.body, 
       { 
        upsert: true //updates if present, else inserts 
       } 
      }) 
     .exec(function(err, answer) { 
      //... 
     }); 
}); 
+0

@talwan再次感謝您的答覆。在'Answer.findOneAndUpdate({ 問題:req.body.question, 員工:req.body.user'事情是不會發生的角的HTTP沒有返回從API側的回答。當我做了'的console.log。 (req.body);'正上方'Answer.findOneAndUpdate'它顯示我的問題,名字和響應來自角發送的實際身體不知道以後 –

+0

此外,雖然員工和響應實體得到保存到數據庫中會發生什麼。答案文件空 –

+0

你是哪裏發出的迴應?難道數據庫條目顯示所有三個領域? –