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

289
Views
Suma de los valores en dictados anidados, excluyendo algunos basados en otra clave en la base de datos MongoDB

Tengo una base de datos MongoDB con el siguiente formato:

 { "_id": xxx, "timestamp": "1643649900000", "scores": [{ "name": "APPL", "price": 80 }, { "name": "GOOGL", "price": 83, }, { "name": "COMPI", "price": 76, }, { "name": "and more names which also can change in the following documents", "price": 76, }] }, { "_id": yyy, "time": "1644350400000", "scores": [{ "name": "STCMP", "price": 33 }, { "name": "APPL", "price": 95, }, { "name": "GOOGL", "price": 83, }, { "name": "MINN", "price": 76, }] },

Necesito sumar todos los precios por vez pero excluyendo (o restando de la suma) algunos.

Mi lista de puntajes tiene alrededor de 200 dictados y quiero excluir alrededor de 5 de ellos. Solo logré hacer la parte de suma, pero después de dos días de búsqueda todavía no puedo excluir con mi conocimiento limitado.

 toBeExcluded = ["APPL", "GOOGL"]
 sums.aggregate([ { "$unwind" : "$scores" }, { "$group": { "_id": "$time", "total": { "$sum": "$scores.price" } } }, { "$addFields":{ "timeAdj": {"$toInt": [{"$subtract":[{"$divide": ["$_id", 1000]}, 300]}]} } }, { "$sort": {"timeAdj":1} } ]))
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

use $cond y verifique los nombres que se excluirán usando $in , en su $sum :

parque infantil mongo

 db.collection.aggregate([ { "$unwind": "$scores" }, { "$group": { "_id": "$time", "total": { "$sum": { "$cond": [ { "$in": [ "$scores.name", [ "APPL", "GOOGL" ] ] }, 0, "$scores.price" ], } } } }, ])
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!