[英]How to push array elements to an array if _id of documents already exists and create new document if _id doesn't exist?
I am using Node.js
, mongoose
, mongodb
, express
and angular
.我正在使用
Node.js
、 mongoose
、 mongodb
、 express
和angular
。 I am saving replies for a survey in a mongoose model.我正在保存对猫鼬模型中调查的答复。 Many people will submit replies for a particular survey.
许多人会针对特定调查提交答复。 When the first person submits a reply for a survey, I want to create a new document for that survey.
当第一个人提交调查回复时,我想为该调查创建一个新文档。 When the second, third.... so on people submit replies for the same survey, I want to add array elements only to the replies array in the following schema.
当第二个、第三个......等人们提交对同一调查的回复时,我只想将数组元素添加到以下架构中的回复数组中。
And when the first person submits a reply for a new survey I want to create a new document for the new survey.当第一个人提交新调查的回复时,我想为新调查创建一个新文档。 How can I do this in mongoose?
我怎么能在猫鼬中做到这一点?
I found similar question in Mongoose.js: how to implement create or update?我在Mongoose.js 中发现了类似的问题:如何实现创建或更新? .
. But, here I want to push the new replies to the next array index of replies[] if the _id is found, else create a new document
但是,如果找到 _id,我想在这里将新回复推送到下一个回复 [] 数组索引,否则创建一个新文档
mongoose model :猫鼬模型:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var MCQReplySchema = new Schema({
_id : String,
surveyname: String,
replies :[{
replierId : String,
answers : [{
questionId : String,
answer : String
}]
}]
});
module.exports=mongoose.model('MCQReply',MCQReplySchema);
Saving data to database :将数据保存到数据库:
router.post("/saveMCQAnswer", function(req, res) {
new MCQReply({
_id : '123',
surveyname: 'sample',
replies :[{
replierId : 'R001',
answers : [{
questionId : 'A001',
answer : 'answer'
}]
}]
}).save(function(err, doc){
if(err) res.json(err);
else
req.flash('success_msg', 'User registered to Database');
res.redirect("/");
});
});
pseudo untested code.未经测试的伪代码。
MCQReply.findOne({_id : the id}, function(err,doc){
if(err) console.log(err);
// if the survey isn't there `doc` will be null then do your save
if(!doc){
new MCQReply({
_id : '123',
surveyname: 'sample',
replies :[{
replierId : 'R001',
answers : [{
questionId : 'A001',
answer : 'answer'
}]
}]
}).save(function(err, doc){
if(err) res.json(err);
else
req.flash('success_msg', 'User registered to Database');
res.redirect("/");
});
}else(doc){
//you get mongoose document
doc.replies.push({replierId : "R001", answer : [{questionId : "A001" ... whatever else you want}]})
//dont forget to save
doc.save(do error check)
}
})
Don't know if this will work but if your having trouble just saving the _id try this for Schema()不知道这是否可行,但如果您在保存 _id 时遇到问题,请尝试将其用于 Schema()
var MCQReplySchema = new Schema({
_id : String,
surveyname: String,
replies :[{
replierId : String,
answers : [{
questionId : String,
answer : String
}]
}]
}, {strict : false});
if {strict :false}
doesn't work如果
{strict :false}
不起作用
try {strict : false, _id :false}
or just _id : false
尝试
{strict : false, _id :false}
或只是_id : false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.