Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
Group if only specific condition fulfil in MongoDB

I want to group some specific records by their pid (only groups) if the number of group events are more than 3.

Example:

Here the number of events for group (pid: 200) is 4 and must be grouped.

- 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: {...}},

Here the group (pid: 200) is grouped and is_too_long: true is added to the record. And group (pid: 800) is not grouped as expected.

- 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},

I tried some other queries but none worked! Please help me!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

  • $facet to separate private and group documents,
  • $group by pid and group document in a array
  • $cond check condition if grouped documents are greater than or equal to 3 then slice single element from array and set is_too_long: true otherwise false
  • $map to iterate loop of docs array and put boolean property is_too_long
  • $unwind deconstruct root array
  • $replaceRoot to replace root object to root
  • $concatArrays, concat private and group array
  • $unwind deconstruct root array
  • $replaceRoot to replace root object in root
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" } }
])

Playground

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda