Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 使用Mongo聚合查询按类型获取最新文档_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

Mongodb 使用Mongo聚合查询按类型获取最新文档

Mongodb 使用Mongo聚合查询按类型获取最新文档,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我有一个mongo集合,我想从中获取一些特定的数据。我需要每种类型的最新文档。数据如下所示: {"id":"100", "type":"A", "date":"2018-01-15T22:19:07.017", "firstName":"Jack", "lastName":"Miller", "age":34} {"id":"101", "type":"B", "date":"2017-12-04T23:28:49.889", "firstName":"Bob", "lastName":"Jac

我有一个mongo集合,我想从中获取一些特定的数据。我需要每种类型的最新文档。数据如下所示:

{"id":"100", "type":"A", "date":"2018-01-15T22:19:07.017", "firstName":"Jack", "lastName":"Miller", "age":34}
{"id":"101", "type":"B", "date":"2017-12-04T23:28:49.889", "firstName":"Bob", "lastName":"Jackson", "age":43}
{"id":"102", "type":"B", "date":"2017-12-11T22:06:33.150", "firstName":"Karen", "lastName":"Decker", "age":12}
{"id":"103", "type":"A", "date":"2017-12-18T21:27:31.847", "firstName":"Sally", "lastName":"Rich", "age":19}
{"id":"104", "type":"A", "date":"2017-12-19T23:07:01.292", "firstName":"Joe", "lastName":"Hunk", "age":11}
{"id":"105", "type":"C", "date":"2018-01-08T21:25:25.715", "firstName":"Tallia", "lastName":"Hope", "age":86}
{"id":"100", "type":"A", "date":"2018-01-15T22:19:07.017", "firstName":"Jack", "lastName":"Miller", "age":34}
{"id":"102", "type":"B", "date":"2017-12-11T22:06:33.150", "firstName":"Karen", "lastName":"Decker", "age":12}
{"id":"105", "type":"C", "date":"2018-01-08T21:25:25.715", "firstName":"Tallia", "lastName":"Hope", "age":86}
因此,每种类型的最新版本如下所示:

{"id":"100", "type":"A", "date":"2018-01-15T22:19:07.017", "firstName":"Jack", "lastName":"Miller", "age":34}
{"id":"101", "type":"B", "date":"2017-12-04T23:28:49.889", "firstName":"Bob", "lastName":"Jackson", "age":43}
{"id":"102", "type":"B", "date":"2017-12-11T22:06:33.150", "firstName":"Karen", "lastName":"Decker", "age":12}
{"id":"103", "type":"A", "date":"2017-12-18T21:27:31.847", "firstName":"Sally", "lastName":"Rich", "age":19}
{"id":"104", "type":"A", "date":"2017-12-19T23:07:01.292", "firstName":"Joe", "lastName":"Hunk", "age":11}
{"id":"105", "type":"C", "date":"2018-01-08T21:25:25.715", "firstName":"Tallia", "lastName":"Hope", "age":86}
{"id":"100", "type":"A", "date":"2018-01-15T22:19:07.017", "firstName":"Jack", "lastName":"Miller", "age":34}
{"id":"102", "type":"B", "date":"2017-12-11T22:06:33.150", "firstName":"Karen", "lastName":"Decker", "age":12}
{"id":"105", "type":"C", "date":"2018-01-08T21:25:25.715", "firstName":"Tallia", "lastName":"Hope", "age":86}
我认为在这里()找到的聚合操作是正确的选择。我尝试使用$group累加器对类型进行分组,然后获取$max date,但不知道如何获取文档的其余部分。我可以很容易地用多个查询来实现这一点,但如果可能的话,我希望使用一个查询


聚合操作是正确的选项吗?也许我错过了一些简单的东西

您可以尝试以下解决方案:

db.test.aggregate([
    { $sort: { "date": -1 } },
    { $group: { _id: "$type", latest: { $first: "$$ROOT" } }},
    { $project : {_id : 0, id : "$latest.id", type : "$latest.type", date : "$latest.date", firstName : "$latest.firstName", lastName : "$latest.lastName", }},
    { $sort: { "type": 1 } }
])
如果您提供此输入:

{"id":"100", "type":"A", "date":"2018-01-15T22:19:07.017", "firstName":"Jack", "lastName":"Miller", "age":34}
{"id":"101", "type":"B", "date":"2017-12-04T23:28:49.889", "firstName":"Bob", "lastName":"Jackson", "age":43}
{"id":"102", "type":"B", "date":"2017-12-11T22:06:33.150", "firstName":"Karen", "lastName":"Decker", "age":12}
{"id":"103", "type":"A", "date":"2017-12-18T21:27:31.847", "firstName":"Sally", "lastName":"Rich", "age":19}
{"id":"104", "type":"A", "date":"2017-12-19T23:07:01.292", "firstName":"Joe", "lastName":"Hunk", "age":11}
{"id":"105", "type":"C", "date":"2018-01-08T21:25:25.715", "firstName":"Tallia", "lastName":"Hope", "age":86}
这是查询生成的所需输出:

{ "id" : "100", "type" : "A", "date" : "2018-01-15T22:19:07.017", "firstName" : "Jack", "lastName" : "Miller" }
{ "id" : "102", "type" : "B", "date" : "2017-12-11T22:06:33.150", "firstName" : "Karen", "lastName" : "Decker" }
{ "id" : "105", "type" : "C", "date" : "2018-01-08T21:25:25.715", "firstName" : "Tallia", "lastName" : "Hope" }

您可以尝试
db.col.aggregate([{$sort:{“date”:-1}},{$group:{{{u id:“$type”,recent:{$first:$$ROOT”}}}])
这正是我要找的。我缺少的是一个$1的项目。谢谢你的帮助!仅供参考,如果您不想列出字段而不是
$project
{$replaceRoot:{newRoot:{newRoot:$latest'}}
的话,您可以使用
$replaceRoot
,哇,这只会越来越好。谢谢@Veeram!