mongodb排序组结果

时间:2016-10-05 19:59:43

标签: mongodb sorting aggregation-framework

我正在尝试获取每个组的第一个,然后我需要对结果集进行排序。我已经实现了第一部分但无法对结果进行排序。这是我试过的

样本数据

    {
    "_id" : ObjectId("57f549e1831529409b000001"),"name" : "book1","author" : "abc","revision" : 1.0,
    "published_on" : ISODate("2016-10-05T18:43:45.902Z"),"publisher" : "newpublisher"
},
{
    "_id" : ObjectId("57f54a4483152940ad000001"),"name" : "book1","author" : "a1","revision" : 1.1,
    "published_on" : ISODate("2016-10-05T18:45:24.436Z"), "publisher" : "newpublisher"
},
{
    "_id" : ObjectId("57f54baa83152940c3000001"), "name" : "oldbook", "author" : "alice","revision" : 1.0,
    "published_on" : ISODate("2016-10-05T18:51:22.484Z"),"publisher" : "newpublisher"
},
{
    "_id" : ObjectId("57f54c4983152940c3000002"),"name" : "artoflearning","author" : "mike","revision" : 1.0,
    "published_on" : ISODate("2016-10-05T18:54:01.585Z"),"publisher" : "oldpublisher"
},
{
    "_id" : ObjectId("57f54c5883152940c3000003"),"name" : "artoflearning","author" : "mike","revision" : 1.1,
    "published_on" : ISODate("2016-10-05T18:54:16.568Z"),"publisher" : "oldpublisher"
},
{   "_id" : ObjectId("57f54c6583152940c3000004"),"name" : "artoflearning","author" : "mike","revision" : 1.2,
    "published_on" : ISODate("2016-10-05T18:54:29.848Z"),"publisher" : "oldpublisher"
},
{ "_id" : ObjectId("57f5513f8315294116000000"),"name" : "learning","author" : "mike","revision" : 2.0,
  "published_on" : ISODate("2016-10-05T19:15:11.342Z"),"publisher" : "newpublisher"
}

我正在获取给定发布者的最新修订版(按desc中的修订版排序)。我用这个查询来实现

Book.collection.aggregate([
{'$match' =>  {"publisher"=>"newpublisher"}},
{'$sort' => {'revision' => -1}},
{'$group' => {'_id' => '$name', 
            'revision' => {'$first' => '$revision'}, 
            'id' => {'$first' => '$_id'},
            'name' => {'$first' => '$name'},
            'published_on' => {'$first' => '$published_on'},
            'publisher' => {'$first' => '$publisher'}
            }
},
{'$project' => {'_id' => '$id', 
                'revision' => '$revision', 
                'name' => '$name',
                'published_on' => '$published_on',
                'publisher' => '$publisher'
              }
},
{'$skip' => 1},
{ '$limit' => 10 }
])

由于我需要分页结果,我已经应用了skip和limit。 我有这个结果

{"_id"=>{"$oid"=>"57f54a4483152940ad000001"}, "revision"=>1.1, "name"=>"book1", "published_on"=>"2016-10-05T18:45:24.436Z", "publisher"=>"newpublisher"}, 
{"_id"=>{"$oid"=>"57f5513f8315294116000000"}, "revision"=>2.0, "name"=>"learning", "published_on"=>"2016-10-05T19:15:11.342Z", "publisher"=>"newpublisher"} 

现在我想按'name'或'published_on'列排序。当我尝试将其应用于上面的结果时,我收到错误

请告知

1 个答案:

答案 0 :(得分:1)

你没有说出你会得到什么错误,这会有所帮助。但你说了

  

现在我想按'name'或'published_on'列排序。当我尝试将其应用于上面的结果时,我收到错误

据我所知,您在sort之后添加了limit。如果您这样做,它只会对跳过和有限的结果进行排序,以便在跳过并限制它们之前对必须调用排序的所有结果进行排序,例如:

Book.collection.aggregate([
  {'$match' =>  {"publisher"=>"newpublisher"}},
  {'$sort' => {'revision' => -1}},
  {'$group' => {'_id' => '$name', 
              'revision' => {'$first' => '$revision'}, 
              'id' => {'$first' => '$_id'},
              'name' => {'$first' => '$name'},
              'published_on' => {'$first' => '$published_on'},
              'publisher' => {'$first' => '$publisher'}
              }
  },
  {'$project' => {'_id' => '$id', 
                  'revision' => '$revision', 
                  'name' => '$name',
                  'published_on' => '$published_on',
                  'publisher' => '$publisher'
                }
  },
  {'$sort' => {'name' => 1}},
  {'$skip' => 1},
  {'$limit' => 10 }
])

我添加了您在集合中发布的文档并运行了此查询,这是结果

[ { _id: 57f5513f8315294116000000,
    revision: 2,
    name: 'learning',
    published_on: Wed Oct 05 2016 16:15:11 GMT-0300 (BRT),
    publisher: 'newpublisher' },
  { _id: 57f54baa83152940c3000001,
    revision: 1,
    name: 'oldbook',
    published_on: Wed Oct 05 2016 15:51:22 GMT-0300 (BRT),
    publisher: 'newpublisher' } ]

published_on排序:

{'$sort': {'published_on': 1}}

[ { _id: 57f54baa83152940c3000001,
    revision: 1,
    name: 'oldbook',
    published_on: Wed Oct 05 2016 15:51:22 GMT-0300 (BRT),
    publisher: 'newpublisher' },
  { _id: 57f5513f8315294116000000,
    revision: 2,
    name: 'learning',
    published_on: Wed Oct 05 2016 16:15:11 GMT-0300 (BRT),
    publisher: 'newpublisher' } ]
相关问题