mongoDB:如何逆转$ unwind

时间:2014-08-05 10:26:27

标签: mongodb mongodb-query aggregation-framework

考虑这组测试结果:

[{
    _id: ObjectId(...),
    name: "Test1",
    acts: [
    {
        name: "act1", 
        tests: [
            {name: "test1", result: true}, 
            {name: "test2", result: true}]
    }]
},
{
    _id: ObjectId(...),
    name: "Test2",
    acts: [
    {
        name: "act1", 
        tests: [
            {name: "test1", result: true}, 
            {name: "test2", result: false}]
    }, 
    {
        name: "act2", 
        tests: [
            {name: "test3", result: true}]
    }]
}]

我正在尝试使用聚合来创建一个带有所有测试结果总和的计算字段,我想要这样的东西:

[{
    _id: ObjectId(...),
    name: "Test1",
    result: true, //new aggregated value
    acts: [
    {
        name: "act1", 
        result: true, //new aggregated value
        tests: [
            {name: "test1", result: true}, 
            {name: "test2", result: true}]
    }]
},
{
    _id: ObjectId(...),
    name: "Test2",
    result: false, //new aggregated value
    acts: [
    {
        name: "act1", 
        result: false, //new aggregated value
        tests: [
            {name: "test1", result: true}, 
            {name: "test2", result: false}]
    }, 
    {
        name: "act2", 
        result: true, //new aggregated value
        tests: [
            {name: "test3", result: true}]
    }]
}]

我尝试过使用aggregate和$ unwind,$ project和$ group:

aggregate([
  {$unwind: "$acts"},
  {$unwind: "$acts.tests"},
  {$project: {name: 1, acts: 1, failed: {$cond: {if: {$eq: ["$acts.tests.test", "true" ]}, then: 0, else: 1}}}},
  {$group: {_id: "$_id", failedCount: {$sum: "$failed"}, acts: {$push: "$acts.tests"}}}
])

但我不能让它反转$ unwind操作,我只得到结果数据结构与原始数据结构不同。 是否可以使结果看起来与原始集合完全相同但具有新的聚合值?

/ gemigspam

1 个答案:

答案 0 :(得分:14)

如何处理这个问题有一个特殊的技巧,但首先如果你有MongoDB 2.6或更高版本,那么你可以在不使用$unwind的情况下实际做到你想要的。如果您要处理大量文档,这对于性能非常方便。

此处的关键操作符$map处理数组,$allElementsTrue运算符将评估您的"结果"领域。使用" map"这里允许测试内部"测试"数组,看看"结果"那里的田地都符合真实的条件。在外部数组的情况下,这个"结果"可以根据需要放入这些文档中,当然,对文档的完整评估遵循相同的规则:

db.test.aggregate([
    { "$project": {
        "name": 1,
        "result": {
            "$allElementsTrue": {
                "$map": {
                    "input": "$acts",
                    "as": "act",
                    "in": {
                        "$allElementsTrue": {
                            "$map": {
                                 "input": "$$act.tests",
                                 "as": "test",
                                 "in": "$$test.result"
                            }
                        }
                    }
                }
            }
        },
        "acts": {
            "$map": {
                 "input": "$acts",
                 "as": "act",
                 "in": {
                    "name": "$$act.name",
                    "result": {
                        "$allElementsTrue": {
                            "$map": {
                                "input": "$$act.tests",
                                "as": "test",
                                "in": "$$test.result"
                            }
                        }
                    },
                    "tests": "$$act.tests"
                 }
            }
        }
    }}
])

在早期版本中执行此操作的方法要求您分两步执行$group以便重建"重建"在对那些"结果"进行测试的同时进行数组运算。再次。此处的另一个区别是使用$min运算符,因为false将被视为比true更小的值并且评估为相同的" allElements"概念:

db.test.aggregate([
    { "$unwind": "$acts" },
    { "$unwind": "$acts.tests" },
    { "$group": {
        "_id": {
            "_id": "$_id",
            "name": "$name",
            "actName": "$acts.name"
        },
        "result": { "$min": "$acts.tests.result" },
        "tests": {
           "$push": {
               "name": "$acts.tests.name",
               "result": "$acts.tests.result"
           }
        }
    }},
    { "$group": {
        "_id": "$_id._id",
        "name": { "$first": "$_id.name" },
        "result": { "$min": "$result" },
        "acts": {
            "$push": {
                "name": "$_id.actName",
                "result": "$result",
                "tests": "$tests"
            }
        }
    }}
])