InMongo db: - 从下面的数据我需要获得基于“employeeId”和“alertTimeStamp”的所有字段的最新记录

时间:2015-09-26 13:41:56

标签: mongodb

示例输入数据: -

 {
                "_id" : ObjectId("56069e78627e6c27e34d3613"),
                "employeeId" : 123456789,
                "alertTimeStamp" : ISODate("2015-09-21T11:59:09Z"),
                "cyclDt" : "2015-09-21",
                "emplcontactType" : "EML",
                "employeesalary" : 10000,
                "creatTimeStamp" : ISODate("2015-09-21T11:59:09Z"),
                "empmsgId" : 23456
        }
        {
                "_id" : ObjectId("56069e78627e6c27e34d3614"),
                "employeeId" : 123456789,
                "alertTimeStamp" : ISODate("2015-09-21T13:59:09Z"),
                "cyclDt" : "2015-09-21",
                "emplcontactType" : "EML",
                "employeesalary" : 10000,
                "creatTimeStamp" : ISODate("2015-09-21T13:59:09Z"),
                "empmsgId" : 23456
        }
        {
                "_id" : ObjectId("56069e78627e6c27e34d3615"),
                "employeeId" : 123456789,
                "alertTimeStamp" : ISODate("2015-09-21T15:59:09Z"),
                "cyclDt" : "2015-09-21",
                "emplcontactType" : "EML",
                "employeesalary" : 10000,
                "creatTimeStamp" : ISODate("2015-09-21T15:59:09Z"),
                "empmsgId" : 23456
        }

我也在查询以下进行分组,但没有得到如何检索所有字段(列)

     db.employee.aggregate(  [ 
    {$match: {"alertTimeStamp":{$gte:ISODate("2015-09-06")}}},
    { $sort: {"_id": 1 ,employeeId: 1, alertTimeStamp: 1} },
    { $group:{  _id:"$_id",_id: {employeeId: "$employeeId"}, alertTimeStamp: { $last: "$alertTimeStamp" }}},
    ])

在输出中,我应该仅低于记录。

             {
                "_id" : ObjectId("56069e78627e6c27e34d3615"),
                "employeeId" : 123456789,
                "alertTimeStamp" : ISODate("2015-09-21T15:59:09Z"),
                "cyclDt" : "2015-09-21",
                "emplcontactType" : "EML",
                "employeesalary" : 10000,
                "creatTimeStamp" : ISODate("2015-09-21T15:59:09Z"),
                "empmsgId" : 23456
        }

请检查并提出建议。没有更多细节。

0 个答案:

没有答案