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

157
Vistas
Update multiple records in a Model with Waterline on Mongodb

Given is an array with book objects, where each status has to be updated:

var books = [ 
              { id: '58b2e58', status: 'pending' },
              { id: '5903703', status: 'pending' } 
                ]

var status = {status: 'accepted'}

I tried this:

Book.update(books, status).exec(function afterwards(err, updatedbooks) {
            if (err) {
                res.json(err)
                }
            console.log("updatedbooks:", updatedbooks);
});

but the 'updatedbooks' log is empty. When checking in mongodb, the books are there, I can find them with Book.find(books);

As here mentioned this here works fine, but I want to have beside the ID's also the status=="pending" in the WHERE criteria.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

please try this.

var bookId= ['1','3'];
var status = {status: 'accepted'}
Book.update({id:bookId, status: 'pending'}, status).exec(function afterwards(err, updatedbooks) {
        if (err) {
            console.log('error in update\n'+err);
            res.json(err)
            }
        else{
            console.log("updatedbooks:", updatedbooks);
        }
});

This should work. Also, if you are getting an error, it will give you an idea about what error you are getting.

over 4 years ago · Santiago Trujillo Denunciar

0

If someone has the correct answer, don't wait. I found a workaround:

The find() function can take criteria like this:

var bookCriteria = [ 
              { id: '1', status: 'pending' },
              { id: '3', status: 'pending' } 
                ]

But the update() function only works with an array with ids:

var bookCriteria = [ '1','3']

So lets first search for books which fulfill our criterias and then take those books and update them.

      Book.find(bookCriteria).exec(function (err, booksFounded) {
            //booksFounded is an array with book objects, with status = 'pending'
            //but we need an array with booksFounded ids as strings

              var bookIDs = booksFounded.map( book => book.id}); //['1','3']

            //now update:

                Book.update(bookIDs , {status:'accepted'}).exec(function afterwards(err, updatedbooks) {
                if (err) {
                    res.json(err)
                    }
                console.log("updatedbooks:", updatedbooks);
                });

   })

Do you have a better solution? With my solution it needs 2 DB transactions, but 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