如何在嵌套猫鼬模式中将输入数组保存到子模式? [英] How to save array of inputs to a child schema in a nested mongoose schema?

查看:74
本文介绍了如何在嵌套猫鼬模式中将输入数组保存到子模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存包含多个问题的调查.每次我输入一个问题和答案并单击保存"按钮时,它都应将其推送到问题数组,最后,当我单击保存调查"按钮时,整个问题应保存在父模式调查"下.

I am trying to save a survey which contains multiples questions. Each time I enter a question and the answers and click the save button it should push it to the question array and at last when I click the save survey button the whole question should be saved under the parent schema 'survey'.

如何使用nodejs和mongoose做到这一点?我尝试过的是这里...

How can I do this with nodejs and mongoose? What I tried is here....

猫鼬模式

 var mongoose = require("mongoose");
 var Schema = mongoose.Schema;

 var SurveySchema = new Schema({
    surveyname: String,
    question  : [{
        que: String,
        ans1: String,
        ans2: String,
        ans3: String,
        ans4: String

        }]
    });

 module.exports=mongoose.model('Survey',SurveySchema);

我将输入保存到模式的js文件

The js file where I save the inputs to the schema

var express = require('express');
var router = express.Router();
var survey = require('../models/QBank');

router.post('/', function(req, res, next){ 
  new survey({
    surveyname: req.body.sname,
    for(var i=0;i<5;i++){
      question.question.push({
        que: req.body.que,
        ans1: req.body.ans1,
        ans2: req.body.ans2,
        ans3: req.body.ans3,
        ans4: req.body.ans4
      });
    }

    }).save(function(err, doc){
      if(err) res.json(err);
      else
          req.flash('success_msg', 'Question saved to QBank');  
      res.redirect("/CreateSurvey");

    });

});

module.exports = router;

我在这里停留我的项目.

I am stuck here with my project.

推荐答案

您可以使用原子更新方法,例如 findOneAndUpdate() 插入单个文档.在这里,您还可以使用本机 $push 操作符进行推送新的问题和问题数组的答案,而不是在可以让mongo完成服务器上的所有工作时使用循环.

You can use an atomic update method like findOneAndUpdate() for your post where you can specify the upsert option. If upsert is true and no document matches the query criteria, findOneAndUpdate() inserts a single document. Here that's where you can also use the native $push operator to push the new question and answers to the question array, rather than using a loop when you can let mongo do all the work on the server.

以下示例显示了如何重构代码:

The following example shows how you can refactor your code:

var express = require('express');
var router = express.Router();
var Survey = require('../models/QBank');

router.post('/', function(req, res, next){ 
    Survey.findOneAndUpdate(
        { "surveyname": req.body.sname }, /* <query> */
        { /* <update> */
            "$push": {
                "question": {
                    "que": req.body.que,
                    "ans1": req.body.ans1,
                    "ans2": req.body.ans2,
                    "ans3": req.body.ans3,
                    "ans4": req.body.ans4
                }
            } 
        },
        { "upsert": true }, /* <options> */
        function(err, doc){ /* <callback> */
            if(err) res.json(err);
            else
                req.flash('success_msg', 'Question saved to QBank');  
            res.redirect("/CreateSurvey");
        }
    );
});

module.exports = router;

在上面,如果<update>参数包含更新运算符表达式,则会创建<query><update>参数的字段和值.更新通过<query>参数中的相等子句创建基础文档,然后应用<update>参数中的更新表达式.

In the above, the fields and values of both the <query> and <update> parameters are created if the <update> parameter contains update operator expressions. The update creates a base document from the equality clauses in the <query> parameter, and then applies the update expressions from the <update> parameter.

这篇关于如何在嵌套猫鼬模式中将输入数组保存到子模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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