mongo组和嵌套文档项目

时间:2018-04-10 05:54:49

标签: mongodb mongodb-query aggregation-framework

我是mongo的新手 我正在使用mongo 3.6 我有一个文档结构的集合:

{
    "_id" : "236165301",
    "boxID" : "431414",    
    "boxName" : "Test",
    "packets" : [ 
        {
            "packetID" : "635665346",
            "packetQty" : "5",
            "packetNum" : "1", 
            "packetItems" : [
                {
                    "packetItemNum" : "Ab89",
                    "packetItemQty" : "1",
                    "packetItemCode" : "6",
                },
                {
                    "packetItemNum" : "Ab90",
                    "packetItemQty" : "3",
                    "packetItemCode" : "6",
                },
                {
                    "packetItemNum" : "Ab87",
                    "packetItemQty" : "3",
                    "packetItemCode" : "8",
                }
            ]
        },
        {
            "packetID" : "635665380",
            "packetQty" : "6",
            "packetNum" : "1", 
            "packetItems" : [
                {
                    "packetItemNum" : "Bc89",
                    "packetItemQty" : "1",
                    "packetItemCode" : "8",
                },
                {
                    "packetItemNum" : "Bc90",
                    "packetItemQty" : "3",
                    "packetItemCode" : "6",
                },
                {
                    "packetItemNum" : "Bc87",
                    "packetItemQty" : "3",
                    "packetItemCode" : "8",
                }
            ]
        }       

    ]
}

我的要求是使用packetItemCode将元素过滤为6,而不会破坏结构

必需输出:

{
    "_id" : "236165301",
    "boxID" : "431414",    
    "boxName" : "Test",
    "packets" : [ 
        {
            "packetID" : "635665346",
            "packetQty" : "5",
            "packetNum" : "1", 
            "packetItems" : [
                {
                    "packetItemNum" : "Ab89",
                    "packetItemQty" : "1",
                    "packetItemCode" : "6",
                },
                {
                    "packetItemNum" : "Ab90",
                    "packetItemQty" : "3",
                    "packetItemCode" : "6",
                }
            ]
        },
        {
            "packetID" : "635665380",
            "packetQty" : "6",
            "packetNum" : "1", 
            "packetItems" : [               
                {
                    "packetItemNum" : "Bc90",
                    "packetItemQty" : "3",
                    "packetItemCode" : "6",
                }
            ]
        }       

    ]
}

我尝试了什么:

db.getCollection('PacketData').aggregate([
    { "$match" : { "_id":"236165301" } },
    { "$unwind" : "$packets" },
    { "$unwind" : "$packets.packetItems" },
    { "$match" : { "packets.packetItems.packetItemCode" : "6" }}
])

但是我无法编写组/项目语句来获得所需的结构输出。 你能帮忙吗

1 个答案:

答案 0 :(得分:1)

在聚合框架中使用$redact运算符来限制数据:

db.PacketData.aggregate([
{ "$match" : { "_id":"236165301" } },
{ $redact:{
   $cond:{
          if:{$or:[{$eq:["$packetItemCode","6"]},{$not:"$packetItemCode"}]},
          then:"$$DESCEND",
          else:"$$PRUNE"

        }

    }}

])

输出是:

/* 1 */
{
"_id" : "236165301",
"boxID" : "431414",
"boxName" : "Test",
"packets" : [ 
    {
        "packetID" : "635665346",
        "packetQty" : "5",
        "packetNum" : "1",
        "packetItems" : [ 
            {
                "packetItemNum" : "Ab89",
                "packetItemQty" : "1",
                "packetItemCode" : "6"
            }, 
            {
                "packetItemNum" : "Ab90",
                "packetItemQty" : "3",
                "packetItemCode" : "6"
            }
        ]
    }, 
    {
        "packetID" : "635665380",
        "packetQty" : "6",
        "packetNum" : "1",
        "packetItems" : [ 
            {
                "packetItemNum" : "Bc90",
                "packetItemQty" : "3",
                "packetItemCode" : "6"
            }
        ]
    }
 ]
}