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

时间:2015-02-12 07:13:12

标签: 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 ??

获得上述输出

3 个答案:

答案 0 :(得分:2)

使用$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
}

答案 1 :(得分:2)

最有效的方法是使用$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
    }}
]

答案 2 :(得分:0)

您好我认为您接近答案但是进行一些更改将符合您的输出

    {
    "$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"
    }
    }
}