I am little confused how to push all objects from array to mongoose array field. Do I have to use loop?
I have created an inventory model where itemlist is an array of itemschema field
const itemlistSchema: mongoose.Schema = new Schema({
item: {
type: Schema.Types.ObjectId,
ref: "Item",
required: true,
},
quantity: {
type: Number,
required: true,
},
});
const inventorySchema: mongoose.Schema = new Schema(
{
code: {
type: String,
trim: true,
required: true,
unique: true,
},
user: {
type: Schema.Types.ObjectId,
ref: "User",
required: true,
},
shop: {
type: Schema.Types.ObjectId,
ref: "Shop",
required: true,
unique: true,
},
itemlist: [itemlistSchema],
},
{ timestamps: true }
);
module.exports = retailMongoose.model(
"Inventory",
inventorySchema,
"Inventory"
);
If I get an array from frontend or as input how can I update this inventory with items.
Suppose I get this array from frontend:
shop_id = "uniqueShopId"
items = [
{
item:"itemId",
quantity:20
},
{
item:"another itemID",
quantity:11
}
]
How can I push this to my inventory model. Every shop will have single inventory , so I can find inventory by shop_id.
Can any one help please? Thank you ❤️
yourmodel.itemListfield and after that call theyourmodel.save()method. Let me know If this solution wouldn't work. This answer also can help you