3

I am trying to remove a product from the user's cart when I delete it from the web, but I don't know how to do it.

The moongoose schema of User is the following:

var schema = mongoose.Schema({
      email: { type: String, required: true },
      password: { type: String, required: true },
      name: { type: String, required: true },
      surname: { type: String, required: true },
      address: { type: String, required: true },
      birth: { type: Date, required: true },
      cartItems: {
          type: [{
              qty: { type: Number, required: true },
              product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product' }
          }]
      }
});

I want to remove an item from cartItems. How can I do it?

1 Answer 1

2
db.collection.update(
  { _id: id },
  { $pull: { 'cartItems.type': { product: '....' } } }
);

more about $pull

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.