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

133
Views
How to remove deeply nested object (Node.js, mongoose)

I'm making a kanban task management app and I'm trying to remove a task with the _id: req.params.id which has the value of 62fa5ae05778ec97bc6ee23a. I tried the following:

const task = await Board.findOneAndUpdate(
    {
      "columns.tasks._id": req.params.id,
    },
    { $pull: { "columns.$.tasks.$._id": req.params.id } },
    { new: true }
  );

But I get the error Too many positional (i.e. '$') elements found in path'columns.$.tasks.$._id'

I searched for a while and came across arrayFilters from the docs but I'm struggling a lot to understand how to implement it for this particular need.

{
    "_id": "62fa5aa25778ec97bc6ee231",
    "user": "62f0eb5ebebd0f236abcaf9d",
    "name": "Marketing Plan",
    "columns": [
        {
            "name": "todo",
            "_id": "62fa5aa25778ec97bc6ee233",
            "tasks": [
                {
                    "title": "Task Four",
                    "description": "This is task four",
                    "subtasks": [
                        {
                            "name": "wash dshes",
                            "completed": false,
                            "_id": "62fa5ae05778ec97bc6ee23b"
                        },
                        {
                            "name": "do homework",
                            "completed": false,
                            "_id": "62fa5ae05778ec97bc6ee23c"
                        }
                    ],
                    "_id": "62fa5ae05778ec97bc6ee23a"
                }
            ]
        },
        {
            "name": "doing",
            "_id": "62fa5aa25778ec97bc6ee234",
            "tasks": []
        },
        {
            "name": "done",
            "_id": "62fa5aa25778ec97bc6ee235",
            "tasks": []
        }
    ],
    "__v": 0
}
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to use $[] positional operator in order to pull from the nested array. Try running this query:

db.Board.updateOne({
    "_id" : "62fa5aa25778ec97bc6ee231",
}, {
    $pull: { 'columns.$[].tasks': { '_id': '62fa5ae05778ec97bc6ee23a' } }
});
about 4 years ago · Santiago Trujillo 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!