Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

470
Views
¿Cómo obtener el número de usuarios registrados cada mes con la consulta de MongoDB?

Estoy tratando de recuperar el número total de usuarios registrados cada mes agrupándolos por año y mes en que se registraron en MongoDB Mi modelo de usuario se ve así

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

El parámetro de fecha es una cadena ISO

la consulta que probé es esta

 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 } }} ])

Pero devuelve una matriz vacía. ¿Cómo puedo resolver esto?

ACTUALIZAR

Como se sugirió, probé estas dos consultas.

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

y

 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 answers
Answer question

0

La consulta de agregación debe ser como la siguiente,

 [ { '$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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!