Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
Find event frequency based on nested object value - MongoDB aggregate

I have the following data schema and trying to find frequency of 'eventType' based on 'country' field. I'm using Mongoose in a Nodejs app.

{
    username: "jack",
    events: [
     {
       eventType: "party",
       createdAt: "2022-01-23T10:26:11.214Z",
       visitorInfo: {
            location: {
                      country: 'US'
                     }
           }
     },
     {
       eventType: "party",
       createdAt: "2022-01-30T10:26:11.214Z",
       visitorInfo: {
            location: {
                      country: 'US'
                     }
           }
     },
     {
       eventType: "party",
       createdAt: "2022-01-29T10:26:11.214Z",
       visitorInfo: {
             location: {
                      country: 'US'
                     }
           }
     },
    {
       eventType: "meeting",
       createdAt: "2022-01-29T10:26:11.214Z",
       visitorInfo: {
             location: {
                      country: 'US'
                     }
           }
     },
    {
       eventType: "meeting",
       createdAt: "2022-01-31T10:16:11.214Z",
       visitorInfo: {
             location: {
                      country: 'GB'
                     }
           }
     },
    {
       eventType: "meeting",
       createdAt: "2022-01-31T10:16:11.214Z",
       visitorInfo: {
             location: {
                      country: 'GB'
                     }
           }
     },
   {
       eventType: "party",
       createdAt: "2022-01-31T10:16:10.214Z",
       visitorInfo: {
             location: {
                      country: 'GB'
                     }
           }
     }
       ]

    }

What I'm trying so far:

 db.collection.aggregate([
  {
   $match: {
   username: "jack"
   }
   },
   {
    $unwind: "$events"
   },
   {
    $match: {
       "events.eventType": "party"
    }
   },

   countries: [
   {
    $group: {
       _id: "$events.visitorInfo.location.country",
       frequency: { $sum: 1 },
       },
     }

    {
      $project: {
        _id: 0,
       countries: "$countries"
       }
     }
    ]
   }
 ])

I'm trying to match on event types here and then group on country. Another approach could be to first get a list of unique countries then find event frequency on each of those countries. Any feedback is appreciated on these approaches.

Basically what I'm hoping to get as result:

countryAndEventFrequency: [
   {
    country: 'US',
    party: 3,
    meeting: 1
   },
  {
    country: 'GB',
    party: 2,
    meeting: 1
   },
]

Appreciate any help or guidance. Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

 db.collection.aggregate([
 {
  $unwind: "$events"
 },
 {
 $project: {
  party: {
    $cond: [
      {
        $eq: [
          "$events.eventType",
          "party"
        ]
      },
      1,
      0
    ]
    },
    meeting: {
     $cond: [
      {
        $eq: [
          "$events.eventType",
          "meeting"
        ]
      },
      1,
      0
       ]
     },
     country: "$events.visitorInfo.location.country"
    }
  },
  {
   $group: {
     _id: "$country",
     meeting: {
       $sum: "$meeting"
     },
    party: {
       $sum: "$party"
     }
   }
  },
  {
   $project: {
    country: "$_id",
    meeting: 1,
     party: 1
   }
   },
   {
   $group: {
    _id: "cc",
     countryAndEventFrequency: {
      $push: "$$ROOT"
    }
   }
   },
   {
    $project: {
  _id: 0,
  countryAndEventFrequency: 1
  }
  }
  ])

explained:

  1. unwind the events array
  2. project to split party & meeting from eventType
  3. group to count the meetings & party by country
  4. project the necessary fields.
  5. Move the documents to the countryAndEventFrequency array

playground

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda