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

136
Views
¿Cómo puedo obtener la suma total de la propiedad mongDB?

Tengo una matriz de objetos con la propiedad UserTo, cada objeto tiene la propiedad Amount, quiero agregar todas las cantidades de usuarios con el mismo UserTo, ¿cómo puedo hacer eso en mongoDB o con javaScript? como puede ver en la matriz a continuación, muchos de ellos tienen el mismo UserTo y cada uno tiene una cantidad diferente, por lo que quiero sumar todas sus cantidades en un solo documento con las cantidades totales y eliminar los duplicados

código:

 (4) [{…}, {…}, {…}, {…}] 0: Amount: 5 CoinpackPurchasedID: "617f96255c47518b6741893c" Level: 1 Status: "Active" UserTo: "617f8d255c47518b674188c9" created_at: "2021-11-01T07:24:36.962Z" updated_at: "2021-11-01T07:24:36.962Z" __v: 0 _id: "617f96345c47518b6741894b" [[Prototype]]: Object 1: Amount: 5 CoinpackPurchasedID: "617f96255c47518b6741893c" Level: 1 Status: "Active" UserTo: "617f8d255c47518b674188c9" created_at: "2021-11-01T07:24:36.962Z" updated_at: "2021-11-01T07:24:36.962Z" __v: 0 _id: "6186558fec66b04ecb6892ce" [[Prototype]]: Object 2: Amount: 5 CoinpackPurchasedID: "617f96255c47518b6741893c" Level: 1 Status: "Active" UserTo: "617f8d255c47518b674188c9" created_at: "2021-11-01T07:24:36.962Z" updated_at: "2021-11-01T07:24:36.962Z" __v: 0 _id: "61867816329e07487e91ea9b" [[Prototype]]: Object 3: Amount: 10 CoinpackPurchasedID: "617f96255c47518b6741893c" Level: 2 Status: "Active" UserTo: "617f8c9d5c47518b674188ba" created_at: "2021-11-01T07:24:36.963Z" updated_at: "2021-11-01T07:24:36.963Z" __v: 0 _id: "617f96345c47518b6741894c" [[Prototype]]: Object
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

usar $group

 db.collection.aggregate([ { "$group": { "_id": "$UserTo", "sum": { "$sum": "$Amount" } } } ])

parque infantil mongo

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!