Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
MongooseJS: How to remove 1 object inside Array in Array of Objects

Hi MongooseJS experts!

I'm new in MongooseJS, This is my 2nd day of solving this problem but I can't find a working solution to this.

Thank you in advance!

My Delete method

Cart.updateOne(
    { "content.merchantId": req.body.merchantId },
    { $pull: { "content.items._id": req.body.productId } },
    { new: true },
    function (error, result) {
      if (error) { }
      else if (result) { }
    }
);

Schema

const CartSchema = new Schema({
      customerId: {
        type: String,
        required: true,
      },
      content: {
        merchantName: {
          type: String,
          required: true,
        },
        merchantId: {
          type: String,
          required: true,
        },
        items: [],
      },
});

Sample JSON

[
    {
        "content": {
            "merchantName": "SAMPLE_MERCHANT_NAME",
            "merchantId": "SAMPLE_MERCHANT_ID",
            "items": [
                {
                    "_id": "SAMPLE_ID",
                    "title": "SAMPLE TITLE",
                }
            ]
        },
        "_id": "618220e83966345ab5d451cd",
        "__v": 0
    },
]

Error message

Cannot use the part (_id) of (content.items._id) to traverse the element ({items: [{ _id: "SAMPLE_ID", title: "SAMPLE TITLE"}] })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you should use like this

db.collection.update({
  "content.merchantId": "SAMPLE_MERCHANT_ID"
},
{
  $pull: {
    "content.items": {
      "_id": "SAMPLE_ID"
    }
  }
},
{
  new:true
},
)

https://mongoplayground.net/p/Ou26tab2mBU

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!