title: Deleting an item with condition in MongoDB?
body: <p>I want to remove a product from the Cart by checking its quantity. Its quantity should be decremented by 1 unless it reaches zero, and after that, it should pull out from the product array of the Cart.</p>
  <p>here is my Logic : (I want to perform the pull and decrement operation inside the single query. But I m stuck on how to perform these two operations together by a simple condition in MongoDb)</p>
  <pre><code>const cart = await Cart.findOneAndUpdate({&quot;products.productId&quot;: req.body.productId}, {$inc: {&quot;products.$.quantity&quot;: -1}}, {new: true})
           
  await Cart.update({&quot;products.productId&quot;: req.body.productId}, {$pull: {quantity: 0}})
  </code></pre>
  <p>here is the model for clarification:</p>
  <pre><code>import mongoose from 'mongoose';
  
  const cartSchema = new mongoose.Schema({
      userId: {
          type: String,
          required: true,
      },
      products: [
          {
              productId: {
                  type: String,
              },
              quantity: {
                  type: Number,
                  default: 1
              }
  
          }
  
      ]
      
  }, {timestamps: true});
  const Cart = new mongoose.model('Cart', cartSchema);
  
  export default Cart;
  </code></pre>
  <p>Thanks :)</p>