代码之家  ›  专栏  ›  技术社区  ›  Vishnu Chauhan

如何使用Meteor按字母顺序对用户进行排序?

  •  1
  • Vishnu Chauhan  · 技术社区  · 6 年前

    我正在使用Meteor用户帐户包。我想按字母升序对所有用户进行排序。我的收藏结构是这样的

    {
        "_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"
        }
    } 
    

    查询是:

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

    我的跳跃是0,极限是25。跳过限制将第二次更改为跳过25和限制25。如何根据用户名对所有用户进行排序?请帮忙。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Vishnu Chauhan    6 年前

    问题是区分大小写的。我认为MongoDB不支持区分大小写的查找和排序。我使用了聚合,最终得到了解决方案。

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