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

473
Visualizações
How to get number of registered users each month with MongoDB query?

I am trying to retrieve the total number of users registered each month grouping them by year and month they registered in MongoDB My user model looks like this

email: String,
  
  gender: String,
  
  password: {
    hash: {
      type: String,
      // required: true
    },
    salt: {
      type: String,
      // required: true
    },
    reset: {
      id: {
        type: String,
        default: "",
      },
      code: {
        type: String,
        default: "",
      },
    },
  },
  name: {
    first: String,
    last: String,
    username: String,
  },
  date: {
    registered: {
      type: Date,
    }
  }

The date parameter is an ISO string

the query that I tried is this

var today = new Date();


            data.newUsersEachMonth=await AccountModel.aggregate([
                { "$match": {
                    "date": {"date.registered": { "$lt": today.toISOString() }}
                    }},
                { "$group": {
                        "_id": {
                            "year": { "$year": "$date" },
                            "month": { "$month": "$date" },
                        },
                        "count": { "$sum": 1 }
                    }}
            ])

But it returns an empty array How can i solve this?

UPDATE

As suggested i tried these two queries

                { "$match": {
                    "date.registered": { "$lt": today.toISOString() }
                    }},
                { "$group": {
                        "_id": {
                            "year": { "$year": "$date.registered" },
                            "month": { "$month": "$date.registered" },
                        },
                        "count": { "$sum": 1 }
                    }}
            ])

and

data.newUsersEachMonth=await AccountModel.aggregate([
                { "$match": {
                    "date.registered": { "$lt": today.toISOString() }
                    }},
                { "$group": {
                        "_id": {
                            "year": { "$year": "$date" },
                            "month": { "$month": "$date" },
                        },
                        "count": { "$sum": 1 }
                    }}
            ])```

Still getting an empty array
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Aggregation query should be like below,

[
  { 
      '$match': {
        'date.registered': { 
            '$lt': today.toISOString() 
        }
     }
  },
  {
    '$project': {
      'month': {
        '$month': '$date.registered'
      }, 
      'year': {
        '$year': '$date.registered'
      }
    }
  }, {
    '$group': {
      '_id': {
        'month': '$month', 
        'year': '$year'
      }, 
      'total': {
        '$sum': 1
      }, 
      'month': {
        '$first': '$month'
      }, 
      'year': {
        '$first': '$year'
      }
    }
  }
]
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