-
First, I’d like to present the case so that everyone can understand what I mean:
https://segmentfault.com/a/1190000004157112
The link above is an ordinary sqlgroup query, but the problem I am facing now is to use mongo for group queries.
Aggregation queries are really killing themselves. Here is the simulation data:
(Please forgive my data, he is bohemian and loves zhi by)
{
"_id" : ObjectId("590c15751f70bd329f8a972d"),
"_class" : "com.birdnest.model.AppPublish",
"user" : "admin",
"deviceType" : "nurse",
"version" : "1.1。 2。 2",
"message": "Van der Sar 3123213213 Van der Sar",
"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": "Van der Sar 3123213213 Van der Sar",
"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": "Van 1123 De Sa 3123213213 Van der Sa",
"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": "Van 1123 De Sa 3123213213 Van der Sa",
"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": "Van 1123 De Sa 3123213213 Van der Sa",
"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": "Van 1123 De Sa 3123213213 Van der Sa",
"creation" : ISODate("2017-05-05T06:02:50.238Z")
}
The demand is this
According to deviceType classification, the latest version number of each classification is obtained.
The result I want is this
/* 1 */
{
"deviceType" : "bed",
"version" :"1.1.112")
}
/* 2 */
{
"deviceType" : "room",
"version" :"1.1.112")
}
/* 3 */
{
"deviceType" : "nurse",
"version" : "1.1.112")
}
The following statement was written by myself, but there are many problems.
Although I have done this, I cannot show the desired results.
db.appPublish.aggregate(
{$group : {_id :"$deviceType",creation : {$max : "$creation"}}}
);
1._id cannot be mapped to an object
2.version cannot be written in group.
I really don’t understand this grammar
Result
/* 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")
}
_id
Of course, there is no real mapping_id
Up, here_id
refer togroup
Key of.
Not sure about yoursversion
Andcreation
What is the relationship?creation
Larger documentsversion
Must it be relatively new? If this condition holds, we can do this:db.question.aggregate([ {$sort: {creation: -1}}, {$group : {_id :"$deviceType", version : {$first : "$version"}}}, {$project: {deviceType: "$_id", version: "$version"}} ]);
$first
That is,group
Then take the first element. And already press in advancecreation
Arrange the order, so the first is the latest element.