mongodb使用查询结果作为搜索条件

时间:2019-08-30 04:01:04

标签: mongodb

数据集:

{ "_id" : ObjectId("5d67bf0e644d83f427bb4917"), "hours" : "2019-08-29 10", "PSCode" : "001", "site" : "A", "faultCode" : "0" }
{ "_id" : ObjectId("5d67bfb1644d83f427bb4918"), "hours" : "2019-08-29 10", "PSCode" : "001", "site" : "B", "faultCode" : "0" }
{ "_id" : ObjectId("5d67c0d1644d83f427bb4919"), "hours" : "2019-08-29 10", "PSCode" : "001", "site" : "C", "faultCode" : "30" }
{ "_id" : ObjectId("5d67c0d1644d83f427bb4920"), "hours" : "2019-08-29 10", "PSCode" : "002", "site" : "A", "faultCode" : "0" }
{ "_id" : ObjectId("5d67c0d1644d83f427bb4921"), "hours" : "2019-08-29 10", "PSCode" : "002", "site" : "B", "faultCode" : "0" }
{ "_id" : ObjectId("5d67c0d1644d83f427bb4922"), "hours" : "2019-08-29 10", "PSCode" : "002", "site" : "C", "faultCode" : "30" }
{ "_id" : ObjectId("5d67c0d1644d83f427bb4923"), "hours" : "2019-08-01 00", "PSCode" : "001", "site" : "A", "faultCode" : "30" }
{ "_id" : ObjectId("5d67c0d1644d83f427bb4924"), "hours" : "2019-08-01 00", "PSCode" : "002", "site" : "A", "faultCode" : "30" }

规则是,如果faultCode的数据为30,则PSCode是不正常的,我只需要注意最新数据,因此我将首先查询最新数据的时间。

db.getCollection("test").aggregate([
    {
        "$group":{
            _id: "$PSCode",
            hours: {"$max": "$hours"}
        }
    }
])

输出:

{ "_id" : "001", "hours" : "2019-08-29 10"}
{ "_id" : "002", "hours" : "2019-08-29 10"}

然后我要使用PSCode_id检查hours是否正常

db.getCollection("test").aggregate([
    {
        "$match": {
            PSCode: "001",
            hours: "2019-08-29 10"
        }
    },
    {
        "$group":{
            _id: "$PSCode",
            faultCode: {"$max": "$faultCode"}
        }
    }
])

输出:

{ "_id" : "001", "faultCode" : 30}

那我该如何通过查询实现它?非常感谢。

2 个答案:

答案 0 :(得分:4)

以下查询可以为我们提供预期的输出:

db.collection.aggregate([
    {
        $sort:{
            "hours":-1,
            "faultCode":-1
        }
    },
    {
        $group:{
            "_id":"$PSCode",
            "PSCode":{
                $first:"$PSCode"
            },
            "faultCode":{
                $first:"$faultCode"
            }
        }
    },
    {
        $project:{  
            "_id":0,
            "PSCode":1,
            "faultCode":1,
            "isNormal":{
                $ne:[
                    "$faultCode",
                    "30"
                ]
            }
        }
    }
]).pretty()

数据集:

{
    "_id" : ObjectId("5d67bf0e644d83f427bb4917"),
    "hours" : "2019-08-29 10",
    "PSCode" : "001",
    "site" : "A",
    "faultCode" : "0"
}
{
    "_id" : ObjectId("5d67bfb1644d83f427bb4918"),
    "hours" : "2019-08-29 10",
    "PSCode" : "001",
    "site" : "B",
    "faultCode" : "0"
}
{
    "_id" : ObjectId("5d67c0d1644d83f427bb4919"),
    "hours" : "2019-08-29 10",
    "PSCode" : "001",
    "site" : "C",
    "faultCode" : "30"
}
{
    "_id" : ObjectId("5d67c0d1644d83f427bb4920"),
    "hours" : "2019-08-29 10",
    "PSCode" : "002",
    "site" : "A",
    "faultCode" : "0"
}
{
    "_id" : ObjectId("5d67c0d1644d83f427bb4921"),
    "hours" : "2019-08-29 10",
    "PSCode" : "002",
    "site" : "B",
    "faultCode" : "0"
}
{
    "_id" : ObjectId("5d67c0d1644d83f427bb4922"),
    "hours" : "2019-08-29 10",
    "PSCode" : "002",
    "site" : "C",
    "faultCode" : "30"
}
{
    "_id" : ObjectId("5d67c0d1644d83f427bb4923"),
    "hours" : "2019-08-01 00",
    "PSCode" : "001",
    "site" : "A",
    "faultCode" : "30"
}
{
    "_id" : ObjectId("5d67c0d1644d83f427bb4924"),
    "hours" : "2019-08-01 00",
    "PSCode" : "002",
    "site" : "A",
    "faultCode" : "30"
}

输出:

{ "PSCode" : "002", "faultCode" : "30", "isNormal" : false }
{ "PSCode" : "001", "faultCode" : "30", "isNormal" : false }

说明:我们正在根据hourfaultCode的降序对文档进行排序。因此,最上面是带有最新小时和最大故障代码的文档。然后,我们可以从列表中为每个PSCode选择第一个文档。

答案 1 :(得分:0)

以下查询匹配所有具有“ faultCode”:30的文档,这表明PSCode不正常。

此后,由于我们只需要最新的文档,因此它将根据“小时”字段对文档进行分组。

获取按“小时”分组的文档后,将根据“小时”字段对结果进行排序,并通过添加限制1(为我们提供最新文档)来限制文档。

db.getCollection("test").aggregate({
    $match:{
        "faultCode": "30"
    },
},
{
    $group:{
        "_id": "$hours",
        records: { $push : "$$ROOT" }
    }
},
{
    $sort: {
        "_id": -1
    }
},
{
    $limit: 1
})

输出:

它包含具有faultCode:30的最新文档,并且都具有“小时”字段的最新值。

{
    "_id" : "2019-08-29 10",
    "records" : [
        {
            "_id" : ObjectId("5d68af0cd74c7263d9f853ab"),
            "hours" : "2019-08-29 10",
            "PSCode" : "001",
            "site" : "C",
            "faultCode" : "30"
        },
        {
            "_id" : ObjectId("5d68af0cd74c7263d9f853ae"),
            "hours" : "2019-08-29 10",
            "PSCode" : "002",
            "site" : "C",
            "faultCode" : "30"
        }
    ]
}