简体   繁体   English

mongoose:将数据插入嵌套对象数组中

[英]mongoose : insert data into an array of nested objects

I am working on a project using node.js mongodb. 我正在使用node.js mongodb进行项目。 My schema somewhat looks like: 我的架构看起来像:

var Doctor = new Schema({
    email : String,
    password : String,
    Dname : String,
    blockAppoint:[{
        day:String,
        sslot:[Number],
        eslot:[Number],
        address:String,
        status1:String
    }]
});

If I take all these values as input from user, I can't figure out how to insert into the array of nested objects. 如果我将所有这些值作为用户的输入,我无法弄清楚如何插入嵌套对象的数组。 If my post api looks like: 如果我的帖子api看起来像:

var doc = new Doctor({
                email : req.body.email,
                password : req.body.password,
                name : req.body.Dname,
                blockAppoint:{
                              status1:req.body.xx,
                              day:req.body.day,
                              sslot:req.body.sslot,
                              eslot:req.body.eslot,
                              address:req.body.address
                            }
                });
doc.save(function(err){
                if(err){
                    res.send(err);
                    return;
                }
                res.json({
                    success: true,
                    message: 'doctor has been added!'   
                });
            });     

I'm able to input just one entry into the database. 我只能在数据库中输入一个条目。 Does anyone know how do I change my api code so as to be able to take read input into my database. 有谁知道如何更改我的api代码,以便能够将读取输入到我的数据库中。

Try adding the values to an array first using the push() method: 首先尝试使用push()方法将值添加到数组中:

var sslot = [], eslot = [], blockAppoint = [];
sslot.push(req.body.sslot);
eslot.push(req.body.eslot);
blockAppoint.push({
    status1: req.body.xx,
    day: req.body.day,
    sslot: sslot,
    eslot: eslot,
    address: req.body.address
});

var doc = new Doctor({
    email: req.body.email,
    password: req.body.password,
    name: req.body.Dname,
    blockAppoint: blockAppoint
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM