排序多个条件MongoDB

时间:2018-05-08 04:45:52

标签: mongodb

我有用户的集合,这是以下文件:

{ "_id": 1, "name": "A", "online": 1, "like": 10, "score": 1 },
{ "_id": 2, "name": "B", "online": 0, "like": 9, "score": 0 },
{ "_id": 3, "name": "C", "online": 0, "like": 8, "score": 1 },
{ "_id": 4, "name": "D", "online": 1, "like": 8, "score": 0 },
{ "_id": 5, "name": "E", "online": 1, "like": 7, "score": 1 },
{ "_id": 6, "name": "F", "online": 0, "like": 10, "score": 1 },
{ "_id": 7, "name": "G", "online": 0, "like": 5, "score": 0 },
{ "_id": 8, "name": "H", "online": 0, "like": 13, "score": 0 }
{ "_id": 9, "name": "I", "online": 0, "like": 6, "score": 0 }

我想显示一些标准的用户列表,并通过一些条件,在线用户和最受欢迎的用户列表排序,在线用户列表显示离线用户得分最多&最喜欢的。以下规则:

  1. 如果online 1必须按like的降序排序。
  2. 如果online0score1,则必须按score的降序进行排序。
  3. 如果online0score0,则必须按like的降序进行排序。
  4. 所以,结果可能是:

    { "_id": 1, "name": "A", "online": 1, "like": 10, "score": 1 },
    { "_id": 4, "name": "D", "online": 1, "like": 8, "score": 0 },
    { "_id": 5, "name": "E", "online": 1, "like": 7, "score": 1 },
    { "_id": 6, "name": "F", "online": 0, "like": 10, "score": 1 },
    { "_id": 3, "name": "C", "online": 0, "like": 8, "score": 1 },
    { "_id": 8, "name": "H", "online": 0, "like": 13, "score": 0 }
    { "_id": 2, "name": "B", "online": 0, "like": 9, "score": 0 },
    { "_id": 9, "name": "I", "online": 0, "like": 6, "score": 0 },
    { "_id": 7, "name": "G", "online": 0, "like": 5, "score": 0 }
    

    我已经完成了第2点,我的查询如下:

    db.users.aggregate([
    {
       $project :
           {
               "id" : 1,
               "name" : 1,
               "online: 1,
               "like" : 1,
               "score" : 1,
               "sort" : {
                   $cond:
                       {
                           "if" :
                               {
                                   $eq : ["$online", true]
                               },
                           "then" : "$like",
                           "else" : "$score"
                       }
               }
           }
    },
    {
       $sort :
           {
               "online" : -1,
               "sort" : -1,
               "id" : 1
           }
    },
    {
       $skip : 0
    },
    {
       $limit : 9
    }
    ])
    

    但我现在有以下结果:

    { "_id": 1, "name": "A", "online": 1, "like": 10, "score": 1 },
    { "_id": 4, "name": "D", "online": 1, "like": 8, "score": 0 },
    { "_id": 5, "name": "E", "online": 1, "like": 7, "score": 1 },
    { "_id": 6, "name": "F", "online": 0, "like": 10, "score": 1 },
    { "_id": 3, "name": "C", "online": 0, "like": 8, "score": 1 },
    { "_id": 2, "name": "B", "online": 0, "like": 9, "score": 0 },
    { "_id": 7, "name": "G", "online": 0, "like": 5, "score": 0 },
    { "_id": 8, "name": "H", "online": 0, "like": 13, "score": 0 }
    { "_id": 9, "name": "I", "online": 0, "like": 6, "score": 0 },
    

    您可以看到,基于第3点,实例{ "_id": 8, "name": "H", "online": 0, "like": 13, "score": 0 }应位于首位,score0

2 个答案:

答案 0 :(得分:0)

首先创建值point

的其他列调用(1 - online)*score

经过以下排序后的数据:

  1. online desc
  2. point desc(在线= 1 point始终为0,在线为0 pointscore
  3. like desc
  4. 您可以使用此查询

        db.yourtable.aggregate(
                [ 
                    { $project:{
                        "id" : 1,
                        "name" : 1,
                        "online": 1,
                        "like" : 1,
                        "score" : 1,
                        point: { $multiply: [ 
                                        {$subtract: [1,"$online"]}                      
                                        , "$score"
                                ]}
    
                        }
                    }
                    ,{ $sort : { online: -1, point : -1, like : -1 } }
                ]
            );
    

答案 1 :(得分:0)

请查看以下查询:

{{1}}