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

157
Views
Grupo si solo se cumple una condición específica en MongoDB

Quiero agrupar algunos registros específicos por su pid (solo grupos) si la cantidad de eventos de grupo es más de 3.

Ejemplo:

Aquí el número de eventos por group (pid: 200) es 4 y deben agruparse.

 - Events - ---------- {_id: ObjectId, type: 'private', pid: 140, data: {...}}, {_id: ObjectId, type: 'group', pid: 800, data: {...}}, // << group 800 {_id: ObjectId, type: 'group', pid: 200, data: {...}}, // << group 200 {_id: ObjectId, type: 'group', pid: 200, data: {...}}, // << group 200 {_id: ObjectId, type: 'private', pid: 140, data: {...}}, {_id: ObjectId, type: 'group', pid: 200, data: {...}}, // << group 200 {_id: ObjectId, type: 'group', pid: 200, data: {...}}, // << group 200 {_id: ObjectId, type: 'private', pid: 130, data: {...}},

Aquí se agrupa el group (pid: 200) y se is_too_long: true al registro. Y el group (pid: 800) no está agrupado como se esperaba.

 - Results - ----------- {_id: ObjectId, type: 'private', pid: 140, data: {...}}, {_id: ObjectId, type: 'group', pid: 800, data: {...}, is_too_long: false}, // << group 800 {_id: ObjectId, type: 'private', pid: 140, data: {...}}, {_id: ObjectId, type: 'group', pid: 200, is_too_long: true}}, // << group 200 (with [is_too_long: true]) {_id: ObjectId, type: 'private', pid: 130, data: {...}, is_too_long: false},

¡Intenté algunas otras consultas pero ninguna funcionó! ¡Por favor, ayúdame!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

  • $facet para separar documentos private y group ,
  • $group por pid y documento de grupo en una matriz
  • $cond condición de verificación si los documentos agrupados son mayores o iguales a 3, luego divida un solo elemento de la matriz y establezca is_too_long: true de lo contrario, falso
  • $map para iterar el bucle de la matriz de docs y poner la propiedad booleana is_too_long
  • $unwind deconstruir matriz root
  • $replaceRoot para reemplazar el objeto root a la raíz
  • $concatArrays , matriz private y group concat
  • $unwind deconstruir matriz root
  • $replaceRoot para reemplazar el objeto root en la raíz
 db.collection.aggregate([ { $facet: { private: [{ $match: { type: "private" } }], group: [ { $match: { type: "group" } }, { $group: { _id: "$pid", root: { $push: "$$ROOT" } } }, { $project: { root: { $cond: [ { $gte: [{ $size: "$root" }, 3] }, { is_too_long: true, docs: { $slice: ["$root", 1] } }, { is_too_long: false, docs: "$root" } ] } } }, { $project: { root: { $map: { input: "$root.docs", in: { $mergeObjects: ["$$this", { is_too_long: "$root.is_too_long" }] } } } } }, { $unwind: "$root" }, { $replaceRoot: { newRoot: "$root" } } ] } }, { $project: { root: { $concatArrays: ["$private", "$group"] } } }, { $unwind: "$root" }, { $replaceRoot: { newRoot: "$root" } } ])

Patio de recreo

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!