MongoDB使用nodeJS插入Json数组

时间:2015-12-17 22:58:21

标签: arrays json node.js mongodb

我正在尝试在JSON对象中继续向此数组添加约会。我的JSON对象如下所示......

{"_id": "travis@hotmail.com","appointment": [{"date": "12/16/2015", "time": "8:40:44pm"}]}

我的代码在git shell中没有显示错误,但是它没有将项添加到数组中,我尝试了db.findById(..),.insert()和其他方法并且无法使它们工作,但是这里是我的代码:

var SchemaApp = new mongoose.Schema({
        _id    : String,
        appointment:[{
            date: String,
            time: String
        }]          
    });
var appointments = mongoose.model('appointment', SchemaApp);
app.post('/makeApp',function(req,res){
  var patient = req.body.appPatient;
  var date = req.body.dateApp;
  var hour = req.body.hour;
  var minutes = req.body.minutes;
  var time = hour+":"+minutes;
  appointments.update(
    {"_id": patient},
    {"$push":{
            "appointment":{
                "date": date,
                "time": time
            }
        }
    }                   
 );
 res.redirect("/admin");
);

如果我在控制台上打印出患者,日期和时间,他们就完全没问题了。

0 个答案:

没有答案