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

139
Visualizações
How to group_by in mongoDb with the output results?

Below is my code..

 group = {
    $group: { 
        "_id": {
            "user_email": "$user_email",
            "sender_email": "$sender_email"
        },
        "count": { "$sum": 1 }

    }
};  


Schema.aggregate(([group]),function(errors,results){

                  console.log(results);             
                  });

my output value is:

[ { _id: 
     { user_email: '123@gmail.com',
       sender_email: '235@gmail.com' },
    count: 5 },
  { _id: 
     { user_email: '123m@gmail.com',
       sender_email: '235@gmail.com' },
    count: 16 },
  { _id: 
     { user_email: '455@gmail.com',
       sender_email: '785@gmail.com' },
    count: 8 },
  { _id: 
     { user_email: '123@gmail.com',
       sender_email: '235@gmail.com' },
    count: 4 } ]

The above record shows the individual poke count.. i need to know that how many user_email has poked sender_email.(an user_email can poke a sender_email for many times but if the sender_email is poked for many times by a user_email i need not want the count as 'many' but i need the count as 1,in the same way if the various users have poked the sender_email i do not want the count of how many times each user has poked sender_email but how many user_email has poked sender_email). I need a mongoDb query for it?

{ user_email: '123@gmail.com',
       sender_email: '235@gmail.com' },
    count: 5 }

123@gmail.com poked 235@gmail.com for 5 times but i need to know the count of how many individual sender_email has been poked by various user_email without passing any parameter?

Below is my collection:

{
    "_id" : ObjectId("590182b3168e590182b315b2"),
    "updatedAt" : ISODate("2017-04-27T12:37:03.385Z"),
    "createdAt" : ISODate("2017-04-27T05:33:39.563Z"),
    "user_email" : "123@gmail.com",
    "sender_email" : "235@gmail.com",
    "status" : 1,
    "__v" : 0
}
{
    "_id" : ObjectId("590182c4a182c4aaa2d21"),
    "updatedAt" : ISODate("2017-04-27T12:37:03.385Z"),
    "createdAt" : ISODate("2017-04-27T05:33:56.122Z"),
    "user_email" : "123@gmail.com",
    "sender_email" : "235@gmail.com",
    "status" : 1,
    "__v" : 0
}
{
    "_id" : ObjectId("590184182c4a1821d98a028"),
    "updatedAt" : ISODate("2017-04-27T12:37:03.385Z"),
    "createdAt" : ISODate("2017-04-27T05:39:40.425Z"),
    "user_email" : "561@gmail.com",
    "sender_email" : "235@gmail.com",
    "status" : 1,
    "__v" : 0
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

group = {
    $group: { 
        "_id": {
            "user_email": "$user_email",
            "sender_email": "$sender_email"
        },
        "_id": {

            "sender_email": "$sender_email"
        },
        "count": { "$sum": 1 }

    }
}; 

My output:

[ { _id: { sender_email: '123@gmail.com' },
    count: 1 },
  { _id: { sender_email: '345@gmail.com' },
    count: 4 } ]

I have changed my query with that i have got the required output.

over 4 years ago · Santiago Trujillo 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