Mongodb多组由

时间:2017-01-25 11:26:29

标签: mongodb mongodb-query aggregation-framework

我有一个像这样的集合

{
  customerid: '1'
  date : "2017-01-20T14:56:59.301Z"
  status: 'active'
}, {
  customerid: '1'
  date : "2017-01-20T16:56:59.301Z"
  status: 'inactive'
}, {
  customerid: '2'
  date : "2017-01-20T12:56:59.301Z"
  status: 'inactive'
}, {
  customerid: '2'
  date : "2017-01-20T13:56:59.301Z"
  status: 'inactive'
}, {
  customerid: '3'
  date : "2017-01-20T12:56:59.301Z"
  status: 'inactive'
}, {
  customerid: '3'
  date : "2017-01-20T22:56:59.301Z"
  status: 'active'
}, {
  customerid: '1'
  date : "2017-01-22T22:56:59.301Z"
  status: 'active'
}, {
  customerid: '1'
  date : "2017-01-21T19:56:59.301Z"
  status: 'active'
}, {
  customerid: '2'
  date : "2017-01-21T17:56:59.301Z"
  status: 'inactive'
}, {
  customerid: '2'
  date : "2017-01-21T18:56:59.301Z"
  status: 'active'
}

现在我想每个客户每天获取最后一次状态 所以对于2017-01-20:我应该得到

{Customerid :1,status :"inactive",date : "2017-01-20T16:56:59.301Z" },
{Customerid :2,status :"inactive",date : "2017-01-20T13:56:59.301Z" },
{Customerid :3,status :"active" ,date : "2017-01-20T22:56:59.301Z"}

(每天基于日期的最后状态)等等

尝试使用group by但不能解决此问题的聚合查询。

<pre>
db.test.aggregate([
{$match : {
               "updateddate" : { $gte: ISODate("2017-01-01T00:00:00.000Z"),$lt: ISODate("2017-01-01T23:59:59.000Z") }}},
    { $sort: { customerid: 1, updateddate: 1 } },
    { $group: {
        _id: { customerid: '$customerid',  year : { $year : "$updateddate" },        
            month : { $month : "$updateddate" },        
            day : { $dayOfMonth : "$updateddate" },status : "$status" }
    }},
    { $group: {
        _id: '$_id.status',
        Count: { $sum: 1 }
    }},
    { $project: {
        _id: 0,
        status: '$_id',
        Count: 1
    }}]
    , function(err, result){
        console.log(result);
    }
);

1 个答案:

答案 0 :(得分:0)

似乎你可以使用$last;

更简单一些
> db.customers.insert([
  {   customerid: '1',   date : ISODate("2017-01-20T14:56:59.301Z"),   status: 'active' },
  {   customerid: '1',   date : ISODate("2017-01-20T16:56:59.301Z"),   status: 'inactive' }, 
  {   customerid: '2',   date : ISODate("2017-01-20T12:56:59.301Z"),   status: 'inactive' }, 
  {   customerid: '2',   date : ISODate("2017-01-20T13:56:59.301Z"),   status: 'inactive' }, 
  {   customerid: '3',   date : ISODate("2017-01-20T12:56:59.301Z"),   status: 'inactive' }, 
  {   customerid: '3',   date : ISODate("2017-01-20T22:56:59.301Z"),   status: 'active' }, 
  {   customerid: '1',   date : ISODate("2017-01-22T22:56:59.301Z"),   status: 'active' }, 
  {   customerid: '1',   date : ISODate("2017-01-21T19:56:59.301Z"),   status: 'active' }, 
  {   customerid: '2',   date : ISODate("2017-01-21T17:56:59.301Z"),   status: 'inactive' }, 
  {   customerid: '2',   date : ISODate("2017-01-21T18:56:59.301Z"),   status: 'active' }
])

> db.customers.aggregate(   
   {$match : {date : { $gte: ISODate("2017-01-01T00:00:00.000Z"), 
                       $lt:  ISODate("2017-01-20T23:59:59.000Z") }}},      
   {$sort: {date:1}},    
   {$group: {_id:"$customerid", status: { $last: "$status"}}} 
)

{ "_id" : "1", "status" : "inactive" }
{ "_id" : "3", "status" : "active" }
{ "_id" : "2", "status" : "inactive" }