I'm pushing subdocuments to an array:
const deliverySchema = new db.Schema({
deliveryId: Number,
amountDelivered: Number,
price: Number
})
const suppliersSchema = new db.Schema({
supplierName: String,
phone: Number,
deliveries: [deliverySchema]
})
const delivery = {
"amountDelivered": 123,
"price": 123
}
Suppliers.updateOne(
{ _id: supplierId },
{ $push: { deliveries: delivery } }
)
How can I have the deliveryId field inside that document auto increment on update. So the result would look something like:
{
"supplierName": "Test supplier",
"phone": 12345678,
"deliveries": [
{
"deliveryId": 1, // Auto increment this field on update
"amountDelivered": 123,
"price": 123
},
{
"deliverId": 2,
"amountDelivered": 123,
"price": 1234
}
]
}