I'm working on a website which we get every day's order in a JS array like this:
let todayOrders = [
{
order_name: "apple",
order_count: 2
},
{
order_name: "orange",
order_count: 3
},
{
order_name: "apple",
order_count: 1
}
];
I need to add every order count to the total order count document which keeps track of every order. So I used a foreach loop and an updateOne method inside it like this:
todayOrders.forEach(function(item, index){
Product.updateOne(
{
product_name: item.order_name
},
{
$inc: {product_count: item1.order_count}
},
);
});
The problem is this code only works fine if the todayOrders does not contain any duplicate order_name which in that case it does not apply all the changes. I believe it has something to do with the async behavior of Mongoose.