Express Mongoose引用未在父文档中放置ObjectId

Express Mongoose引用未在父文档中放置ObjectId,express,mongoose,reference,Express,Mongoose,Reference,我的Mongoose模型没有将ObjectId放置在父文档中以引用另一个集合中的子文档。我该怎么做 这些是我的模型: menu_items.js var menuItems = new mongoose.Schema({ title : String, subitem: [{type: mongoose.Schema.Types.ObjectId,ref: 'sub_items'}] }, {collection: 'menu_items'}); module.exports = mo

我的Mongoose模型没有将ObjectId放置在父文档中以引用另一个集合中的子文档。我该怎么做

这些是我的模型:

menu_items.js

var menuItems = new mongoose.Schema({
  title : String,
  subitem: [{type: mongoose.Schema.Types.ObjectId,ref: 'sub_items'}]
}, {collection: 'menu_items'});

module.exports = mongoose.model("menu_items", menuItems);
sub_items.js

var subItems = new mongoose.Schema({
  title: String,
},{collection: 'sub_items'});

module.exports = mongoose.model("sub_items", subItems);
ExpressJS中的我的子项post函数:

postController.postSubitems = function(req,res,item) {
  var id = req.body.id;
  var saveData =  {
    title: req.body.sub_item
  };
  var data = new item(saveData);
  saveToDB(data,res);
};

您还需要更新您的菜单项。例如,假设新子项的_id为123456。您需要更新菜单项,如下所示:

menuItem.subitem.push(123456);
menuItem.save();
这将把_id添加到子项数组中,从而为菜单项提供对指定子项的引用