Mongoose – 如何在子子文档数组中添加对象?

我可以将项目添加到子文档数组中,但是我无法将项目添加到子子文档数组中。

这是我的Mongoose模式:

var botSchema = { "name" : String, "type" : String, "keyword" : String, "active" : Boolean, "cron" : String }; var apiSchema = { "name" : String, "key" : String, "secret" : String, bots:[botSchema] }; var userSchema = { "email" : String, "password" : String, apis : [apiSchema] }; module.exports.User = mongoose.model('user',userSchema); 

我如何添加一个bot项目到现有的API项目?

向用户添加API时,我没有任何问题。 我这样做:

  User.findByIdAndUpdate( userId, { $push: { apis: api } }, { safe: true, upsert: true }, function(err, model) { console.log(err); console.log(model); } ); 

但是我正在尝试所有将bot添加到现有的API。 我试过这个:

 User.findOneAndUpdate({ "_id": userId, "apis._id": apiId }, { $push: { "apis.$.bots": bot } }, function(err, doc) { console.error("Error:", err); console.log("Doc:", doc); } ); 

但是我得到这个错误显示:

CastError:强制转换为未定义的值失败“[object Object]”