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 9172033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:14:54+00:00 2026-06-17T16:14:54+00:00

I have a User model var User = mongoose.model(‘Users’, mongoose.Schema({ username: ‘string’, password: ‘string’,

  • 0

I have a User model

var User = mongoose.model('Users',
    mongoose.Schema({
        username: 'string',
        password: 'string',
        rights: 'string'
    })
);

I want to get all the users, sorted alphabetically by username. This is what I have tried

User.find({}, null, {sort: {username: 1}}, function (err, users) {
    res.send(users);
});

However, this does not sort the users alphabetically. How can I sort alphabetically?

EDIT: I got confused because I was expecting a “purely alphabetically” sort from Mongoose, not one where Z > a. Basically, I wanted a sort based on username.toLowerCase().

  • 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-17T16:14:56+00:00Added an answer on June 17, 2026 at 4:14 pm

    EDIT: Per the comment the issue turns out to be sorting on toLowerCase(username). MongoDB doesn’t have a built in method for complex sorting. So there are essentially two ways to go:

    1. Add a usernameLowerCase field to the Schema. This is the better option if you need to do this a lot.
    2. Perform an aggregation with a projection using the $toLower operator to dynamically generate a usernameLowerCase field. This comes with performance and memory caveats, but it may be the more convenient choice.

    Original Answer: Here’s a complete example that sorts correctly using the specific code from the question. So there must be something else going on:

    #! /usr/bin/node
    
    var mongoose = require('mongoose');
    mongoose.connect('localhost', 'test');
    var async = require('async');
    
    var User = mongoose.model('Users',
        mongoose.Schema({
            username: 'string',
            password: 'string',
            rights: 'string'
        })
    );
    
    var userList = [
        new User({username: 'groucho', password: 'havacigar', rights: 'left'}),
        new User({username: 'harpo', password: 'beepbeep', rights: 'silent'}),
        new User({username: 'chico', password: 'aintnosanityclause', rights: 'all'})
    ];
    
    async.forEach(userList, 
        function (user, SaveUserDone) {
            user.save(SaveUserDone);
        },
        function (saveErr) {
            if (saveErr) {
                console.log(saveErr);
                process.exit(1);
            }
            User.find({}, null, {sort: {username: 1}}, function (err, users) {
                if (err) {
                    console.log(err);
                    process.exit(1);
                }
                console.log(users);
                process.exit(0);
            });
        }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Mongoose User model: var User = mongoose.model('Users', mongoose.Schema({ username: 'string', password:
In mongoose I've got this model: var userschema = new mongoose.Schema({ user: String, following:
I have some Mongoose Models with geospacial indexes: var User = new Schema({ name
This is my schema: var userschema = new mongoose.Schema({ user: String, imagen: [{ title:
I have this model: var HuntGroupSchema = new Schema({ name : { type: String,
I have to following code: var UserSchema = mongoose.Schema({ email: { type: String, unique:
I have a MongoDb schema like this var User = new Schema({ UserName: {
I have a user model that requires the user to change their password every
I have a Mongoose schema (excerpt below): var PersonSchema = new Schema({ name :
I have a User model(Backbone.js) and I want to update its settings attribute and

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.