Express 如何向mongoDB模型添加对象数组

Express 如何向mongoDB模型添加对象数组,express,mongoose,Express,Mongoose,我有一个代表餐厅菜单的模型,在该模型中,我有一个菜单项数组。我不知道如何使用express将菜单项添加到我的菜单模型中,我在不确定要写什么的地方加粗了一行 这是菜单模型: const mongoose = require("mongoose"); const Schema = mongoose.Schema; const FoodItemSchema = new Schema({ name: { type: String, required: true }, pri

我有一个代表餐厅菜单的模型,在该模型中,我有一个菜单项数组。我不知道如何使用express将菜单项添加到我的菜单模型中,我在不确定要写什么的地方加粗了一行

这是菜单模型:

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

const FoodItemSchema = new Schema({
  name: { type: String, required: true },
  price: { type: Number, required: true },
  Category: { type: String, required: false },
  Quantity: { type: String, required: false },
});

const MenuSchema = new Schema({
  restaurant_id: { type: mongoose.Schema.Types.ObjectId, ref: "restaurant" },
  items: [FoodItemSchema],
  dateCreated: { type: String, required: false },
});

module.exports = mongoose.model("menu", MenuSchema);
这是我的express函数,我正在使用它向数据库中添加菜单,我不确定该为函数中的菜单项数组添加什么

exports.sendMenuData = (req, res) => {
  const menu = new Menu({
    restaurant_id: req.body.restaurant_id,
    **items: [FoodItemSchema]**, //Not sure what to write here in terms of req 
    dateCreated:req.dateCreated,
  });
  menu
    .save()
    .then((data) => {
      console.log(data);
      res.send(data);
    })
    .catch((err) => {
      console.log(err);
    });
};

你需要通过邮递员或任何你正在使用的应用程序来访问你的数据库和食物列表

例如:

items: [req.body.theNameYouWant],
邮递员:

你想要的名字:{名字:“热狗”,价格:3,…(你必须把所有的食物都放在这里)}


否则,您也可以使用$addToSet$push等运算符,这将允许您在数组中引入FoodItemSchema。

您在请求中发送什么?我假设某种JSON数据包含食物项目?感谢您的帮助,如果我想使用postman一次添加多个食物项目,我该怎么做。您可以使用,after},然后再次打开括号。