Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:07:40+00:00 2026-06-11T11:07:40+00:00

While exploring mongoose for nodejs I ran into the problem of needing to know

  • 0

While exploring mongoose for nodejs I ran into the problem of needing to know the amount of user in my collection:

My collection has records, each record has a user. I want to know the amount of unique (different) users.

How can I do this with mongoose?

EDIT:

The database is growing quite fast, is there anyway to get the number back from the DB instead of getting all the distinct records and counting them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:07:41+00:00Added an answer on June 11, 2026 at 11:07 am

    Here’s an alternative answer as I get an exception when I try Reddest’s approach with Mongoose 3.1.2 (which seems like a bug in Mongoose to me as Reddest’s approach should be fine).

    You can call the distinct method on your collection’s model, specifying the name of the user-identifying field of that collection:

    Record.distinct('user_id').exec(function (err, user_ids) {
        console.log('The number of unique users is: %d', user_ids.length);
    });
    

    Or if you want to chain the distinct call from a find, include the callback in the distinct call (this did work for me):

    Record.find().distinct('user_id', function (err, user_ids) { ... });
    

    UPDATE

    If you just want the count without getting the values, stick a count() call in the chain:

    Record.distinct('user_id').count().exec(function (err, count) {
        console.log('The number of unique users is: %d', count);
    });
    

    NOTE: this doesn’t work in the latest Mongoose code (3.5.2).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I here by post the code why I came across while exploring one technique.
I want to know how to add images while exporting the image from highcharts.
While deveoping a site (using Forms authentication and InProc sessionstate) a frequently run into
While recently exploring prezi I realized there have to be better tools than PowerPoint
I've been exploring WPF and XAML for a while now, but have hit a
I'm trying to solve a two-dimensional random walk problem from the book, exploring python.
I have an ASP.NET website that generates documents. Each document takes a while to
While exploring various ways of computing factorials in Clojure, I came up with the
While exploring some solutions to my previous question about the inner workings of Python
While exploring msdn sites ,most of the condition checking places they are using (NULL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.