I have a schema like this
const orderfoodSchema = new Schema({
_id: [Schema.Types.ObjectId],
name: reqString,
quantity: reqInt,
price: reqInt,
});
and then I want to save it like this
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();
but it throws an error that _id can't be an Array now why would the mongoose.Types.ObjectID returns an Array and how can I fix this?
CodePudding user response:
You don't need to define _id for the schema. It will be auto-generated in MongoDB. Try to remove _id from your schema or remove [] from your schema. try to code like _id: Schema.Types.ObjectId
would not generate an array of ids.