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

249
Visualizações
Find total of registered user in the last 7 days using mongoose

I'm storing my data in a schema like this

enter image description here

I wonder how i can create a query in mongoose to get count of created user in the last 7 days where I expect my output to be like this

{ day1: 4, day2: 4, day3: 9, day4: 12, day5: 7, day6: 0, day7: 10 }

sorry for my bad english

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

const usersCount = await User.countDocuments({
  created_at: { $lt: new Date('2022-04-08'), $gt: new Date('2022-04-01') }
});

This will give you count for users created in last seven days. Dynamic date you can generate in your express backend.

over 4 years ago · Santiago Trujillo Relatório

0

Hello I used mongo aggregate to group the data you need daily like this:

const toDate = new Date("2022-04-08");

const fromDate = new Date("2022-04-01");

const data = await User.aggregate([{
    $match: {
      createdAt: {
        $gte: new Date(fromDate),
        $lte: new Date(toDate),
      },
    },
  },
  {
    $sort: {
      createdAt: -1
    }
  },
  {
    $project: {
      day: {
        $dayOfMonth: "$createdAt"
      },
      month: {
        $month: "$createdAt"
      },
      year: {
        $year: "$createdAt"
      },
    },
  },
  {
    $group: {
      _id: {
        day: "$day",
        year: "$year",
        month: "$month",
      },
      count: {
        $sum: 1
      },
    },
  },
  {
    $project: {
      _id: 0,
      day: "$_id.day",
      month: "$_id.month",
      year: "$_id.year",
      count: "$count",
    },
  },
]);

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