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

216
Views
Cómo actualizar datos en MongoDB cuando se selecciona el cuerpo marcado

¿Cómo puedo actualizar los datos en MongoDB cuando marco la casilla de verificación sin enviar ningún formulario?

mi esquema

 const userSchema = new mongoose.Schema({ name: { type: String, trim: true, }, todos: [ { task: { type: String, trim: true, required: 'Please Enter your Task', }, dueDate: { type: Date, default: new Date(+new Date() + 3 * 24 * 60 * 60 * 1000), }, dueTime: String, done: { type: Boolean, default: false, }, }, ], });

Quiero actualizar el elemento done que está en la matriz de todos .

Traté de hacer esto.

JavaScript del lado del cliente principal

 $(document).ready(function () { $('.todo--checkbox').change(function () { let isChecked; if (this.checked) { isChecked = true; $.ajax({ url: '/todo/' + this.value, type: 'PUT', data: { done: true }, }); } else { isChecked = false; $.ajax({ url: '/todo/' + this.value, type: 'PUT', data: { done: false }, }); } }); });

En el front-end, he establecido el valor de la casilla de verificación en el _id del objeto.

/routes/index.js aquí estoy manejando mis rutas

 router.put('/todo/:id', todoControllers.checkStatus);

Y finalmente estoy manejando ese controlador en mi todoCOntroller.js

 exports.checkStatus = async (req, res) => { try { const user = await User.aggregate([ { $unwind: '$todos' }, { $match: { 'todos._id': req.params.id } }, ]); // res.json(user); console.log(user); } catch (err) { console.log('error: ', err); } };

Pero no recibo ningún user en mi consola.

Por favor, dime dónde me equivoco.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No es necesario utilizar agregado. Puedes hacerlo usando $elemMatch

 const user = await User.find({ todos: { $elemMatch: { _id: req.params.id } }, });

Para obtener más información, lea los documentos

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!