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

248
Views
expresa la suma de agregación de mongoDB

Tengo muchos registros en mi colección mongodb y necesito contar alguna información.

formato de registros:

 { "_id" : someId, "targetFrom" : ObjectId("603e0355e805140334e79438"),<-- this is ID for search "targetTo" : null, "operationPaid" : true, "type" : "coming", <--- type "moneyAccount" : someId, "agent" : null, "sum" : 5000, <--- sum } { "_id" : someId, "targetFrom" : null, "targetTo" : null, "operationPaid" : true, "type" : "out", <--- type "moneyAccount" : someId, "agent" : ObjectId("603e0355e805140334e79438"),<-- this is ID for search "sum" : 3000, <--- sum }

entonces, necesito agrupar por TIPO de registros y obtener SUM para id ObjectId ("603e0355e805140334e79438"), pero la identificación para la búsqueda puede ser el campo targetFrom o targetTo o agent

para este ejemplo necesito obtener el resultado 2000

viene sum 5000 y sale sum 3000 con

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Consulta

  • haga coincidir el Id en uno de los 3 campos posibles
  • grupo por nulo (toda la colección 1 grupo), si type="out" , resto el campo de suma, de lo contrario, agrego al campo de suma

Código de prueba aquí

 aggregate( [{"$match": {"$expr": {"$or": [{"$eq":["$targetFrom", ObjectId("603e0355e805140334e79438")]}, {"$eq":["$targetTo", ObjectId("603e0355e805140334e79438")]}, {"$eq":["$agent", ObjectId("603e0355e805140334e79438")]}]}}}, {"$group": {"_id":null, "sum": {"$sum": {"$cond": [{"$eq":["$type", "out"]}, {"$subtract":[0, "$sum"]}, "$sum"]}}}}])
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!