【发布时间】:2017-11-17 15:10:19
【问题描述】:
我正在构建一个简单的应用程序,一家公司在其中向员工发送问题以征求反馈意见。我正在努力将更多员工(用户)作为数组放入问题文档中。现在它只插入一名员工。基本上我需要的是让员工对每个问题做出回应,并且能够(将来)查询数据库以获取员工回答的所有问题。这是我的架构。
这里是previous issue that was solved - 任何感兴趣的人。
模型
var QuestionSchema = Schema({
id : ObjectId,
title : String,
employees : [{ type: ObjectId, ref: 'User'}]
});
module.exports = mongoose.model('Question', QuestionSchema);
var UserSchema = Schema({
username : String,
response : String,
questions : [{ type: ObjectId, ref: 'Question'}]
});
module.exports = mongoose.model('User', UserSchema);
api.js
Question.findOne({ title: 'Should we buy a coffee machine?'}).exec(function(err, question) {
//example: is this the right way of creating the array
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 to answer >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
}
【问题讨论】:
标签: arrays node.js mongodb express mongoose