如果文档_id已经存在,如何将数组元素推到数组中,如果_id不存在,如何创建新文档? [英] How to push array elements to an array if _id of documents already exists and create new document if _id doesn't exist?

查看:52
本文介绍了如果文档_id已经存在,如何将数组元素推到数组中,如果_id不存在,如何创建新文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node.jsmongoosemongodbexpressangular. 我在猫鼬模型中保存了对调查的答复.许多人会针对特定调查提交答复.当第一个人提交调查问卷答复时,我想为该调查问卷创建一个新文档.当第二个,第三个...等人提交针对同一调查的答复时,我只想将数组元素添加到以下架构中的答复数组中.

I am using Node.js, mongoose, mongodb, express and 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?

我在 Mongoose.js中发现了类似的问题:如何实现创建或更新?. 但是,如果找到了_id,我想在这里将新回复推送到下一个回复数组索引[],否则请创建一个新文档

猫鼬模型:

 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);

将数据保存到数据库:

      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("/");

    });

   });

推荐答案

伪未经测试的代码.

    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)
        }


    })

不知道这是否行得通,但是如果您在保存_id时遇到麻烦,请尝试使用Schema()

Don't know if this will work but if your having trouble just saving the _id try this for Schema()

var MCQReplySchema = new Schema({ 

    _id : String,
    surveyname: String,
    replies :[{
        replierId : String,
        answers : [{
            questionId : String,
            answer : String
        }]  
    }]

    }, {strict : false});

如果{strict :false}不起作用

尝试{strict : false, _id :false}或仅_id : false

这篇关于如果文档_id已经存在,如何将数组元素推到数组中,如果_id不存在,如何创建新文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆