如何使用 mongoDB 从集合中找到不同的(和最大的)值?

How to find distinct (and greatest) values from collection using mongoDB?

我有以下合集 -

[{
    "customerId" : "54a32e9f1e14fa5476d654db",
    "hostId" : "192.168.20.20",
    "runtimeMilliSeconds" : 1422007201815
}
{
    "customerId" : "54a32e9f1e14fa5476d654db",
    "hostId" : "192.168.20.20",
    "runtimeMilliSeconds" : 1422008101736
}
{
    "customerId" : "54a32e9f1e14fa5476d654db",
    "hostId" : "192.168.20.21",
    "runtimeMilliSeconds" : 1422009002239
}
{
    "customerId" : "54a32e9f1e14fa5476d654db",
    "hostId" : "192.168.20.21",
    "runtimeMilliSeconds" : 1422009901379
}
{
    "customerId" : "54a32e9f1e14fa5476d654db",
    "hostId" : "192.168.20.22",
    "runtimeMilliSeconds" : 1422010801685
}
{
    "customerId" : "54a32e9f1e14fa5476d654db",
    "hostId" : "192.168.20.22",
    "runtimeMilliSeconds" : 1422010801585
}]

我还有 hostIds 列表:[ "192.168.20.20" , "192.168.20.21" , "192.168.20.22"]

我想将 hostId 列表与集合进行匹配并查找最新(最大)runtimeMilliSeconds 以获得以下输出 -

[{"hostId":"192.168.20.20", "runtime": 1422007201815},
{"hostId":"192.168.20.21", "runtime": 1422009002239},
{"hostId":"192.168.20.22", "runtime": 1422010801685}]

我已经尝试使用 mongo 聚合 -

{ "$match" : { "hostId" : { "$in" : [ "192.168.20.20" , "192.168.20.21" , "192.168.20.22"]} ,
"customerId" : "54a32e9f1e14fa5476d654db"}},
{ "$sort" : { "runtimeMilliSeconds" : -1}},
{ "$group" : { "_id" : { "hostId" : "$hostId" , 
"runtime" : "$runtimeMilliSeconds"}}},
{ "$project" : { "hostId" : "$_id.hostId" ,
 "runtimeMilliSeconds" : "$_id.runtime" , "_id" : 0}}

但它给了我集合中的所有值。

如何使用 mongo 获得上述输出?

您好,我认为您接近您的答案,但进行一些更改后将满足您的输出

    {
    "$match": {
    "hostId": {
        "$in": [
            "192.168.20.20",
            "192.168.20.21",
            "192.168.20.22"
        ]
    },
    "customerId": "54a32e9f1e14fa5476d654db"
    }
},
{
    "$group": {
    "_id": {
        "hostId": "$hostId",
        "runtime": "$runtimeMilliSeconds"
    }
    }
},
{
    "$sort": {
    "_id.runtime": -1
    }
}{
    "$group": {
    "_id": "$_id.hostId",
    "runtime": {
        "$first": "$_id.runtime"
    }
    }
}

使用$first运算符

db.test.aggregate(
[
   { "$match" : { "hostId" : { "$in" : [ "192.168.20.20" , "192.168.20.21" , "192.168.20.22"]} , "customerId" : "54a32e9f1e14fa5476d654db"}},
   { "$sort" : { "runtimeMilliSeconds" : -1}},
   { "$group" : { "_id" : { "hostId" : "$hostId" } , "runtime" : { $first : "$runtimeMilliSeconds" }}},   
   { "$project" : { "hostId" : "$_id.hostId" , "runtimeMilliSeconds" : "$runtime" , "_id" : 0}}
]
)

输出将是:

{
    "result" : [ 
        {
            "hostId" : "192.168.20.20",
            "runtimeMilliSeconds" : 1422008101736
        }, 
        {
            "hostId" : "192.168.20.21",
            "runtimeMilliSeconds" : 1422009901379
        }, 
        {
            "hostId" : "192.168.20.22",
            "runtimeMilliSeconds" : 1422010801685
        }
    ],
    "ok" : 1
}

最有效的方法是使用 $max 运算符(不需要 $sort 阶段):

[
    {"$match" : {
        "hostId" : { "$in" : [ "192.168.20.20" , "192.168.20.21" , "192.168.20.22"]},
        "customerId" : "54a32e9f1e14fa5476d654db"
    }},
    { "$group" : {
        "_id" : "$hostId",
        "runtime" : {"$max" : "$runtimeMilliSeconds"}
    }},
    {"$project" : {
        "hostId" : "$_id" ,
        "runtime" : 1,
        "_id" : 0
    }}
]