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

194
Views
Tomar el recuento de un campo específico en la base de datos

Así que tengo un esquema y en ese esquema hay un objeto llamado "caseCount" con un valor numérico. Simplemente quiero obtener todos los valores para cada objeto de "caseCount" y sumarlos. ¿Podría alguien señalarme en la dirección correcta? Si necesito explicar algo más, con gusto lo haré. ¡Gracias!

Esquema:

 const Profile = Schema({ userID: String, messageCount: Number, caseCount: Number, Skins: Array, });

código:

 const data = Profiles.collection.aggregate([{ $group: { _id: '$id', totalCaseCount: { $sum: '$caseCount' } } }]);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Demostración: https://mongoplayground.net/p/7TnpQl3tJHt

Use $sum en la consulta de agregación.

$grupo

 db.collection.aggregate({ $group: { _id: null, toatalCaseCount: { $sum: "$caseCount" } } })

 Profiles.collection.aggregate([{ $group: { _id: null, totalCaseCount: { $sum: '$caseCount' } } }], function(err, data) { console.log(data); // here you'll get your data from the query });
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!