title: how to add product to Shopping Cart with nodejs express and mongoose
body: <p>I am building an inventory management app, and I want to create a functionality that resembles a shopping cart/product system. I am at the stage of adding a product to the cart, and can't seem to figure out a working code. </p>
  
  <pre><code>const mongoose = require("mongoose");
  
  const CartSchema = new mongoose.Schema(
    {
      active: Bolean,
      modifiedOn: Date,
      product: [
        {
          qunantity: String,
          name: String,
          price: number
        }
      ]
    },
    { timestamps: true }
  );
  
  module.exports = mongoose.model("Cart", CartSchema);
  </code></pre>
  
  <p>Cart controller :</p>
  
  <pre><code>exports.postCart = asyncHandler(async (req, res, next) =&gt; {
    let cart = JSON.parse(req.body.cart);
    if (!cart) return res.json(products)
    for (var i = 0; i &lt; products.length; i++) {
      id = products[i].id.toString();
      if (cart.hasOwnProperty(id)) {
        products[i].qty = cart[id]
        products.push(products[i]);
      }
    }
    return res.json(products);
  })
  </code></pre>
  
  <p>Am trying to set up an upsert like function such that document is created for the provided userId and if one does not already exists. The truth is that i have tried but cant figure it out,anyone with an idea will be appreciated</p>