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

174
Views
campo de matriz de conteo de población de mangosta

Quiero obtener objetos con población y campo de la matriz que quiero contar.
Por ejemplo:

 const ChildSchema = new mongoose.Schema({ name: String, age: Number, siblings: [{type: ObjectId, ref: 'Child'}], toys: [{type: ObjectId, ref: 'Toy'}] })

y quiero obtener una población de los hermanos y contar los juguetes en un objeto, así:

 const Dan = { name: "Dan", age: 4, siblings: [ { name: "Sally", age: 7 }, { name: "Ben", age: 10 }, { name: "Emily", age: 2 } ], numOfToys: 11 }

Ya tengo esto:

 const returnedChild = await ChildModel.findById(BenId) .populate('siblings', 'name age') .select('name age siblings') .lean()

¿Cómo incluyo el recuento del juguete en el objeto devuelto?

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

0

Solo usa esquemas virtuales:

 ChildSchema.virtual('toyCount').get(function () { return this.toys.length }); const returnedChild = await ChildModel.findById(BenId) .populate('siblings', 'name age') .select('name age siblings') .lean() console.log("Toys -> ", returnedChild.toyCount)
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!