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

151
Views
Actualice varios registros en un modelo con línea de flotación en Mongodb

Se da una matriz con objetos de libro, donde cada status debe actualizarse:

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

Intenté esto :

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

pero el registro de 'libros actualizados' está vacío. Al registrarme en mongodb, los libros están ahí, puedo encontrarlos con Book.find(books);

Como se mencionó aquí, esto aquí funciona bien, pero quiero tener junto a las ID también el estado = = "pendiente" en los criterios DONDE.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

por favor intente esto

 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); } });

Esto debería funcionar. Además, si recibe un error, le dará una idea de qué error está recibiendo.

over 4 years ago · Santiago Trujillo Report

0

Si alguien tiene la respuesta correcta, no espere. Encontré una solución:

La función find() puede tomar criterios como este:

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

Pero la función update() solo funciona con una matriz con identificadores:

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

Así que primero busquemos libros que cumplan con nuestros criterios y luego tomemos esos libros y actualícelos.

 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); }); })

¿Tienes una mejor solución? Con mi solución necesita 2 transacciones DB, pero funciona.

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!