MongoDB查询嵌套文档列表

时间:2014-01-08 14:47:32

标签: mongodb

我不是MongoDB的新手,但对聚合概念不熟悉...... 我有收集数据,看起来像这样,目前它包含2个文件

 {
    "_id" : ObjectId("52cc0b079f0ae55e9fb770f8"),
    "uid" : 100,
    "data" : {
        "mi" : [ 
            {
                "miId" : NumberLong(1),
                "name" : "ABC",
                "severity" : "HIGH",
                "failures" : NumberLong(2),
                "description" : "Some description",
                "remediation" : "Some remedy"
            }, 
            {
                "miId" : NumberLong(10),
                "name" : "PQR",
                "severity" : "HIGH",
                "failures" : NumberLong(3),
                "description" : "Some description",
                "remediation" : "Some remedy"
            }
}

{
    "_id" : ObjectId("52cc0b079f0ae55easdas8"),
    "uid" : 200,
    "data" : {
        "mi" : [ 
            {
                "miId" : NumberLong(10),
                "name" : "ABC",
                "severity" : "HIGH",
                "failures" : NumberLong(20),
                "description" : "Some description",
                "remediation" : "Some remedy"
            }, 
            {
                "miId" : NumberLong(18),
                "name" : "PQR",
                "severity" : "HIGH",
                "failures" : NumberLong(30),
                "description" : "Some description",
                "remediation" : "Some remedy"
            }
      }
}

如何在MongoDB shell或Java中创建基于“name”的groupby()并总结所有“失败”的查询,结果还应该包含最高“name”的“uid” “失败”。 结果应如下所示:

{
{
    "_id" : ObjectId("508894efd4197aa2b9490741"),
    "name" : "ABC",
    "sum_total_of_failures" : 22,
    "uid" : 200
}

{
    "_id" : ObjectId("508894efd4197aa2b9490741"),
    "name" : "PQR",
    "sum_total_of_failures" : 33,
    "uid" : 200
}
}

任何帮助都会非常感激,我用$ unwind写了一个查询,因为“mi”文档存储在列表中,但它返回了空结果。 查询如下:

    db.temp.aggregate(
{$unwind: "$mi"}, 
{$project: {mi : "$mi"}},
{$group: { _id: "$name",total: { $sum: "$failures" }}})

1 个答案:

答案 0 :(得分:1)

尝试以下查询:

db.collection.aggregate(
{$unwind : "$data.mi"},
{$sort : {"data.mi.failures" : -1}},
{$group : {_id : "$data.mi.name", 
           sum_total_of_failures : {$sum : "$data.mi.failures"}, 
           uid : {$first : "$uid"}}}
)

结果如下:

"result" : [
    {
        "_id" : "PQR",
        "sum_total_of_failures" : NumberLong(33),
        "uid" : 200
    },
    {
        "_id" : "ABC",
        "sum_total_of_failures" : NumberLong(22),
        "uid" : 200
    }
]

使用Java驱动程序,您可以按照以下方式执行此操作:

    DBCollection coll = ...

    DBObject unwind = new BasicDBObject("$unwind", "$data.mi");
    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("data.mi.failures", -1));

    DBObject groupObj = new BasicDBObject();
    groupObj.put("_id", "$data.mi.name");
    groupObj.put("sum_total_of_failures", new BasicDBObject("$sum", "$data.mi.failures"));
    groupObj.put("uid", new BasicDBObject("$first", "$uid"));

    DBObject group = new BasicDBObject("$group", groupObj);

    AggregationOutput output = coll.aggregate(unwind, sort, group);
    if (output != null) {
        for (DBObject result : output.results()) {
            String name = (String) result.get("_id");
            Long sumTotalOfFailures = (Long) result.get("sum_total_of_failures");
            Integer uid = (Integer) result.get("uid");
        }
    }