• Home
  • Jobs
  • Courses
  • Teachers
  • For business
  • Blog
  • ES/EN

0

32
Views
Remove a object from array using MongoDB

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?

13 days ago ·

Santiago Trujillo

1 answers
Answer question

0

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

more about $pull

13 days ago · Santiago Trujillo Report
Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.