数据
聚合查询实在是要了老命了,下面是模拟数据:
{
"_id" : ObjectId("590c15751f70bd329f8a972d"),
"_class" : "com.birdnest.model.AppPublish",
"user" : "admin",
"deviceType" : "nurse",
"version" : "1.1。2。2",
"message" : "范德萨312313213213范德萨",
"creation" : ISODate("2017-05-05T06:02:29.300Z")
}
{
"_id" : ObjectId("590c15751f70bd329f8a972e"),
"_class" : "com.birdnest.model.AppPublish",
"user" : "admin",
"deviceType" : "nurse",
"version" : "1.1。2。2",
"message" : "范德萨312313213213范德萨",
"creation" : ISODate("2017-05-05T06:02:29.447Z")
}
{
"_id" : ObjectId("590c157b1f70bd329f8a972f"),
"_class" : "com.birdnest.model.AppPublish",
"user" : "admin",
"deviceType" : "bed",
"version" : "1.1。2。2",
"message" : "范1123德萨312313213213范德萨",
"creation" : ISODate("2017-05-05T06:02:35.731Z")
}
{
"_id" : ObjectId("590c157c1f70bd329f8a9730"),
"_class" : "com.birdnest.model.AppPublish",
"user" : "admin",
"deviceType" : "bed",
"version" : "1.1。2。2",
"message" : "范1123德萨312313213213范德萨",
"creation" : ISODate("2017-05-05T06:02:36.164Z")
}
{
"_id" : ObjectId("590c158a1f70bd329f8a9733"),
"_class" : "com.birdnest.model.AppPublish",
"user" : "admin",
"deviceType" : "room",
"version" : "1.1.112",
"message" : "范1123德萨312313213213范德萨",
"creation" : ISODate("2017-05-05T06:02:50.082Z")
}
{
"_id" : ObjectId("590c158a1f70bd329f8a9734"),
"_class" : "com.birdnest.model.AppPublish",
"user" : "admin",
"deviceType" : "room",
"version" : "1.1.112",
"message" : "范1123德萨312313213213范德萨",
"creation" : ISODate("2017-05-05T06:02:50.238Z")
}
需求
按照deviceType分类,获得其中每个分类最新的一个版本号
结果举例
/* 1 */
{
"deviceType" : "bed",
"version" :"1.1.112")
}
/* 2 */
{
"deviceType" : "room",
"version" :"1.1.112")
}
/* 3 */
{
"deviceType" : "nurse",
"version" : "1.1.112")
}
下面这个语句是我自己写的,但是存在很多问题
db.appPublish.aggregate(
{$group : {_id :"$deviceType",creation : {$max : "$creation"}}}
);
1._id不能映射到对象上
2.version不能写在group中
实在有点弄不懂这个语法
结果
/* 1 */
{
"_id" : "bed",
"creation" : ISODate("2017-05-05T06:02:44.750Z")
}
/* 2 */
{
"_id" : "room",
"creation" : ISODate("2017-05-05T06:02:50.397Z")
}
/* 3 */
{
"_id" : "nurse",
"creation" : ISODate("2017-05-05T06:02:29.447Z")
}
db.appPublish.aggregate([
{$group : {_id :{deviceType:"$deviceType,"version:"$version",},creation : {$max : "$creation"}}},
{$project:{_id:1,creation:1}}
]);
楼上的结果
/* 1 */
{
"_id" : {
"deviceType" : "room",
"version" : "1.1.112"
},
"creation" : ISODate("2017-05-05T06:02:50.397Z")
}
/* 2 */
{
"_id" : {
"deviceType" : "nurse",
"version" : "1.1122"
},
"creation" : ISODate("2017-05-05T06:02:16.959Z")
}
/* 3 */
{
"_id" : {
"deviceType" : "nurse",
"version" : "1.1。2。2"
},
"creation" : ISODate("2017-05-05T06:02:29.447Z")
}
/* 4 */
{
"_id" : {
"deviceType" : "bed",
"version" : "1.1。2。2"
},
"creation" : ISODate("2017-05-05T06:02:36.164Z")
}
/* 5 */
{
"_id" : {
"deviceType" : "bed",
"version" : "1.1.112"
},
"creation" : ISODate("2017-05-05T06:02:44.750Z")
}
db.appPublish.aggregate({$group:{_id:'$deviceType',version:{$max:'$version'}}})
1._id不能映射到对象上:在映射对象的属性上通过注解指定_id
2.version不能写在group中:如上可以在group中写version_