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

242
Views
How to find a collection inside another collection in mongoose

Im trying to delete a specific document that is inside a collection that is also inside another collection. For example: I have a collection called todoTitle and inside it has an array of another collection called todo, I want to find that specific todo collection that is inside the array and delete it. Im trying to use findOneAndDelete but it's not working. here is the code:

const TodoSchema = new mongoose.Schema({
    name:  String,
})
const todo = mongoose.model('todo', TodoSchema)

const TodoTitleSchema = new mongoose.Schema({
    title: String,
    content: [TodoSchema],
})
const todoTitle = mongoose.model('todoTitle', TodoTitleSchema)

app.post('/removeTodo',(req,res)=>{
    let todoID = req.body.removeTodo;
    todoTitle.findOneAndDelete({content:[{_id: todoID}]}, (err, foundtodo)=>{
        console.log(foundtodo);
        if(err){
            console.log(err)
        }
        res.redirect('/');
    })
})
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Hello so I finally figured it out, I had to write this code:

todoTitle.findOneAndUpdate({_id: listID}, {$pull: {content: {_id: todoID}}}, (err, foundtodo)=>{
        if(err){
            console.log(err)
        }
    })

and the error was actually caused by the ID being undefined so I fixed that part up and now it works.

over 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!