mongoose.Types.ObjectId 返回了一个数组

mongoose.Types.ObjectId returned an Array

我有这样的模式

const orderfoodSchema = new Schema({
  _id: [Schema.Types.ObjectId],
  name: reqString,
  quantity: reqInt,
  price: reqInt,
});

然后我想这样保存

const order = new Order();
  order.key_id = mongoose.Types.ObjectId();
  order.id_table = (
    await Table.findOne({
      id_table: {
        $nin: (
          await Order.find({ status: "not paid" })
        ).map((id) => id.id_table),
      },
    })
  ).id_table;
  order.date = new Date();
  order.total_cost = 0;
  order.status = "not paid";
  await order.save();

但它抛出一个错误,即 _id 现在不能是一个数组 为什么 mongoose.Types.ObjectID returns 是一个数组,我该如何解决这个问题?

您不需要为架构定义 _id。在 MongoDB 中将是 auto-generated。尝试从您的模式中删除 _id 或从您的模式中删除 [] 。尝试像 _id: Schema.Types.ObjectId 这样的代码不会生成 ID 数组。