mongodbmeteormeteor-accounts

How can sort alphabetically of users using Meteor?


I am using meteor user accounts package. I want to sort all users according to alphabetically ascending order. My Collection Structure is like this

{
    "_id" : "6J3jjZB7DMRyPcTxh",
    "createdAt" : ISODate("2018-05-28T13:07:03.428Z"),
    "services" : {
        "password" : {
            "bcrypt" : "$2b$10$1W2g1Ceal39uUz0JVZ1JSuT1M9gKfKas.5VZ8ThH2Ga6cPp7SY6zO"
        },
        "resume" : {
            "loginTokens" : []
        }
    },
    "emails" : [ 
        {
            "address" : "vishnu@gmail.com",
            "verified" : false
        }
    ],
    "profile" : {
        "username" : "vishnu singh",
        "regPhone" : "8088***0297",
        "discount" : "66.66",
        "isDeleted" : false,
        "role" : "user"
    }
} 

Query is :

    responseArray = Meteor.users.find({ 'profile.isDeleted': { $ne: true }, 'profile.role': { $ne: 'admin' } }, { sort: { 'profile.username': 1 }, skip: skip, limit: limit }).fetch();

My skip is 0 and limit is 25. Skip limit change the second time to skip 25 and limit 25. How can sort all users according to the username? please help.


Solution

  • The issue is with case sensitive. I think MongoDB doesn't support find and sort with case sensitive. I used aggregation and finally get the solution.

      responseArray = Meteor.users.aggregate([{
                        $match: {
                            'profile.isDeleted': { $ne: true },
                            'profile.role': { $ne: 'admin' }
                        }
                    },
                    {
                        "$project": {
                            profile: 1,
                            emails: 1,
                            "insensitive": { "$toLower": "$profile.username" }
                        }
                    },
                    { "$sort": { "insensitive": 1 } },
                    { "$skip": skip },
                    { "$limit": limit }
                ])