Mongodb Query: Latest record by date for each item

谁说我不能喝 提交于 2019-12-07 07:29:12

问题


There are six devices in a collection, each has many records, some have records of new dates and some have week or/and month older. I need a query which returns latest last record of each device. In the case of .aggregate() I need that complete "data" filed.

Here is the sample json.

{
"date_time" : some-date
"device_id" : 27,
"gateway_id" : 1,
"data": [{"r" : 203,"v" : 3642},{"r" : 221,"v" : 3666}]
}
{
"date_time" : some-date
"device_id" : 28,
"gateway_id" : 1,
"data": [{"r" : 203,"v" : 3002},{"r" : 221,"v" : 3006}]
}
{
"date_time" : some-date
"device_id" : 29,
"gateway_id" : 1,
"data": [{"r" : 203,"v" : 3002}, {"r" : 221,"v" : 3006}]
}

I tried a lot of queries but no success.

Please not the following are not for this case.

db.col.find({"device_id": {"$in": devices}, "date_time": { "$gte": last_date}}) .sort({$natural: -1})

db.col.find({"device_id": {"$in": devices}, "date_time": { "$gte": last_date}}) .sort({$natural: -1}).limit(1)

db.col.find({"device_id": {"$in": devices}}) .sort({"date_time": -1}).limit(1)

db.col.find({"device_id": {"$in": devices}}) .sort({"_id": -1}).limit(1)

I am looking for the query which provides only latest record of every device in collection. Please note that in the case of .aggregate() I need that complete "data" filed.


回答1:


Try with following snippet

db.collection.aggregate([
            {$group: {
                    "_id": "$device_id",
                    "gateway_id": {"$last":"$gateway_id"},
                     data: {$last: '$data'},
                     date: {$last: '$date_time'},
                }},
            {$project: {
                     "device_id": "$_id",
                      "gateway_id": "$gateway_id",
                      "data": "$data",
                      "date_time": "$date"
                  }},
            {$sort: {
                    'date': -1
                }}
        ])

In above query group by device id and date, data, and gateway_id will be latest in each row.

Output is-

{
    "result" : [ 
        {
            "_id" : 29,
            "gateway_id" : 1,
            "data" : [ 
                {
                    "r" : 203,
                    "v" : 3002
                }, 
                {
                    "r" : 221,
                    "v" : 3006
                }
            ],
            "device_id" : 29,
            "date_time" : "a"
        }, 
        {
            "_id" : 28,
            "gateway_id" : 1,
            "data" : [ 
                {
                    "r" : 203,
                    "v" : 3002
                }, 
                {
                    "r" : 221,
                    "v" : 3006
                }
            ],
            "device_id" : 28,
            "date_time" : "b"
        }, 
        {
            "_id" : 27,
            "gateway_id" : 1,
            "data" : [ 
                {
                    "r" : 203,
                    "v" : 3642
                }, 
                {
                    "r" : 221,
                    "v" : 3666
                }
            ],
            "device_id" : 27,
            "date_time" : "a"
        }
    ],
    "ok" : 1
}

Thanks



来源:https://stackoverflow.com/questions/29368141/mongodb-query-latest-record-by-date-for-each-item

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!