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

130
Views
MongDB Agrega dos valores con el mismo nombre

tengo la siguiente función agregada en mi código para contar cuántas veces se encuentra un valor en la base de datos:

 let data: any = await this.dataModel.aggregate( [ { $match: { field: new ObjectID(fieldID), }, }, { $group: { _id: "$value", total_for_value: { $sum: 1 }, }, }, ] );

Esto funciona correctamente, sin embargo, mi configuración de datos es un poco diferente. Tengo dos tipos de campos de valor. Algo como esto:

 { "_id" : ObjectId("123"), "value" : "MALE" }

y algunos como este:

 { "_id" : ObjectId("456"), "value" : { "value" : "MALE", } }

¿Hay alguna manera de agrupar aquellos en los que _id y _id.value son iguales? De momento los cuenta por separado.

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

0

db.collection.aggregate([ { "$addFields": { "key2": { "$cond": { "if": { $and: [ { "$eq": [ { "$type": "$key" }, "object" ] } ] }, "then": "$key.value", "else": "$key" } } } }, { "$group": { "_id": "$key2", "data": { $push: "$$ROOT" } } } ])

Esto haría el trabajo si _id.value es un objeto.

Patio de juegos

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!