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

185
Views
Encontrar un registro por recordId y userId al actualizar un valor dentro de una matriz, usando Mongose

Lo siento si el título es un poco confuso, pero no estoy seguro de cómo reformularlo.

Así que tengo el siguiente objeto:

 { "_id": "622dec2e037d7ae286b366c9", "name": "Moriel Odeski", "email": "moriel96@gmail.com", "street": "Ben Gurion", "city": "Be'er Sheva", "zipcode": 329886, "tasks": [ { "id": "1", "title": "Test", "completed": true }, { "id": "2", "title": "Test2", "completed": false }, { "id": "3", "title": "Test3", "completed": false }, { "id": "4", "title": "Do The Dishes", "completed": false } ], "posts": [ { "id": 1, "title": "That's Moriel's Post", "body": "According to my evaluations 0-0" }, { "id": 2, "title": "Something about something", "body": "That's interesting" } ] }

Estoy tratando de escribir una función que obtenga un ID de taskID y un ID de tarea y, al llamarlo, cambia el userID de la tarea a true donde id = taskID .completed tarea y donde ID de userId : userID de usuario

Tengo la siguiente función que no funciona:

 const markTaskCompleted = (userID , taskID) => { return new Promise((resolve, reject) => { User.findOneAndUpdate({id : userID , tasks : {$elemMatch: {id : taskID}}}, {$set: {"tasks.$.completed" : true}}, {"new" : true , "safe" : true , "upsert" : true} , function(err , data) { if(err){reject(err)} else { resolve(data.tasks) } }) }) }

lanza:

 Plan executor error during findAndModify :: caused by :: The positional operator did not find the match needed from the query.

¿Dónde me equivoqué con esto?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Me resulta difícil leer su código porque no se ve limpio... así que tal vez primero sea mejor hacerlo más limpio... ¿supongo?

 const markTaskCompleted = async (userID , taskID) => await User.findOneAndUpdate({ id: userID, tasks: {$elemMatch: {id: taskID}} }, {$set: {"tasks.$.completed": true}}, {"new": true , "safe": true , "upsert": true}, })

Usaste la devolución de llamada dentro de la devolución de llamada, por lo que no me pareció limpio. async/await . Espero que esto te parezca más limpio a ti también.

El documento oficial de mongoose también usa el método await for findOneAndUpdate() . https://mongoosejs.com/docs/tutorials/findoneandupdate.html#getting-started

Échale un vistazo. ¿Puedes comprobar si el código que escribí funciona?

about 4 years ago · Juan Pablo Isaza 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!