Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda