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

192
Views
Mongodb: ¿Cómo pasar otra etapa dentro de $group?


Rendimiento esperado:

 [ { "_id": "healty", "doc_count": 2, "ingredients": { "Leaves": { "1.2g": 1, "1.5g": 1 }, "Spinach": { "12g": 1, "18g": 1 } } }, { "_id": "junk", "doc_count": 3, "ingredients": { "cheese": { "100g": 1, "120g": 2 }, "meat": { "50g": 1, "60g": 1, "70g": 1 } } } ]

Agregación a continuación: Playground 1

 db.collection.aggregate([ { $group: { "_id": "$type", // grouping the document by "type" field "ingredients": { $push: "$$ROOT" //want to run Playground2 aggrgtion in each of it }, "doc_count": { $sum: 1 // total count } } } ])


Después,

También descubrí un Otro paso para convertir la matriz de ingredientes después: Playground 2

Pero, esta agregación es para Todos los documentos. Quiero hacer que la agregación de Playground2 funcione para el campo de ingredientes solo en cada objeto de grupo .

Es similar a combinar Playground1 y Playground2 . ¿Cómo hago esto?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

db.collection.aggregate([ { "$set": { "ingredients": { "$objectToArray": "$ingredients" } } }, { "$unwind": "$ingredients" }, { "$group": { "_id": { type: "$type", ingredient: "$ingredients" }, "count": { "$sum": 1 }, "doc_count": { "$addToSet": "$item" } } }, { "$group": { "_id": { type: "$_id.type", ingredient: "$_id.ingredient.k" }, "docs": { "$push": { k: "$_id.ingredient.v", v: "$count" } }, "doc_count": { "$push": "$doc_count" } } }, { "$group": { "_id": "$_id.type", "ingredients": { "$push": { k: "$_id.ingredient", v: { "$arrayToObject": "$docs" } } }, "doc_count": { "$push": { $reduce: { input: "$doc_count", initialValue: [], in: { $concatArrays: [ "$$value", "$$this" ] } } } } } }, { "$set": { "ingredients": { "$arrayToObject": "$ingredients" }, doc_count: { "$size": { "$setUnion": { $reduce: { input: "$doc_count", initialValue: [], in: { $concatArrays: [ "$$value", "$$this" ] } } } } } } } ])

parque infantil mongo

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!