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

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

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 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!