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

246
Views
la mangosta obtiene solo esos documentos si todos los subdocumentos pasan los criterios

Digamos que tengo un esquema de Lesson y contiene una serie de subdocumentos de question . ¿Cómo obtendría esas lecciones donde está marcado como completado pero todas las preguntas en la matriz se han isDeleted: true

Pensé en algo como

Lesson.find({ isComplete: true, 'questions.isDeleted': true })

pero eso obtiene las lecciones si se elimina al menos una de las preguntas. ¿Cómo afirmaría la condición para todas las preguntas?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No puede usar la dot notation de puntos ya que aplana la matriz. Lo que puede hacer es usar $expr , esto permite el uso de aggregation operators , como $reduce .

Ahora podemos iterar sobre la matriz y ver si todos los documentos cumplen las condiciones, así:

 db.collection.find({ isComplete: true, $expr: { $eq: [ { $reduce: { input: "$questions", initialValue: 0, in: { $sum: [ "$$value", { $cond: [ { $ne: [ "$$this.isDeleted", true ] }, 1, 0 ] } ] } } }, 0 ] } })

Parque infantil Mongo

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!