Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 查询嵌套文档mongoDB_Arrays_Mongodb_Mongodb Query - Fatal编程技术网

Arrays 查询嵌套文档mongoDB

Arrays 查询嵌套文档mongoDB,arrays,mongodb,mongodb-query,Arrays,Mongodb,Mongodb Query,这是我的文本模式MongoDB示例 我想检索每个用户的数据。我还需要_id,因为在一个完整的文档中,它有几个结构类似的_id 我尝试使用“展开”操作符,因为对象包含如下嵌套数组: db.getCollection('topic_stats_2').aggregate([{ $unwind : "$usages.type.users.text" }, { $unwind : "$usages.type.users.stats.xyz1" }, { $unwind : "$usages.type.u

这是我的文本模式MongoDB示例

我想检索每个用户的数据。我还需要_id,因为在一个完整的文档中,它有几个结构类似的_id

我尝试使用“展开”操作符,因为对象包含如下嵌套数组:

db.getCollection('topic_stats_2').aggregate([{ $unwind : "$usages.type.users.text" }, { $unwind : "$usages.type.users.stats.xyz1" }, { $unwind : "$usages.type.users.stats.xyz2" }, { $unwind : "$usages.type.users.stats.xyz3" }, { $unwind : "$usages.type.users.xyz4" }, { $unwind : "$usages.type.users.xyz5" }])
但结果是零

我想要这样的表格格式的结果。我知道该表将包含大量冗余数据。但这正是我想要的

_id  | count | xyz4 | xyz5 | xyz1 | xyz2 | xyz3 | text | type |
     |       |      |      |      |      |      |      |      |
     |       |      |      |      |      |      |      |      |



/* 1 */
{
    "_id" : “photo",
    "count" : 236,
    "usages" : [ 
        {
            "type" : 1,
            "users" : [ 
                {
                    "text" : “jkncfkjfdn",
                    "stats" : {
                        “xyz1" : 6,
                        “xyz2" : 1,
                        “xyz3" : 1194
                    },
                    “xyz4" : "julius babao",
                    “xyz5" : "juLiusbabao"
                }, 
                {
                    "text" : “fcnf",
                    "stats" : {
                        “xyz1" : 9,
                        “xyz2" : 6,
                        “xyz3" : 1199
                    },
                    “xyz4" : "Dman",
                    “xyz5" : "DmanTheDesigner"
                }, 
                {
                    "text" : “dckejsndc",
                    "stats" : {
                        “xyz1" : 1,
                        “xyz2" : 0,
                        “xyz3" : 1200
                    },
                    “xyz4" : "EastmanHouse",
                    “xyz5" : "EastmanHouse"
                }
            ]
        }, 
        {
            "type" : 2,
            "users" : [ 
                {
                    "text" : “msdnc",
                    "stats" : {
                        “xyz1" : 1,
                        “xyz2" : 1,
                        “xyz3" : 1168
                    },
                    “xyz4" : "Shayne",
                    “xyz5" : "RKTay"
                }, 
                {
                    "text" : “kfjnvfv",
                    "stats" : {
                        “xyz1" : 0,
                        “xyz2" : 0,
                        “xyz3" : 523
                    },
                    “xyz4" : "andy stitches",
                    “xyz5" : "myproudmendes"
                }, 
                {
                    "text" : “jkopoiuyt",
                    "stats" : null,
                    “xyz4" : "jm",
                    “xyz5" : "jihannelayosa"
                }
            ]
        }, 
        {
            "type" : 3,
            "users" : [ 
                {
                    "text" : “opted",
                    "stats" : {
                        “xyz1" : 58,
                        “xyz2" : 32,
                        “xyz3" : 1192
                    },
                    “xyz4" : "♪♫Lil Darryl♫♪",
                    “xyz5" : "LilDarryl301"
                }, 
                {
                    "text" : "Cloud 9",
                    "stats" : {
                        “xyz1" : 1,
                        “xyz2" : 1,
                        “xyz3" : 1171
                    },
                    “xyz4" : "FGN",
                    “xyz5" : "pretty_brown66"
                }, 
                {
                    "text" : "Cloud 9",
                    "stats" : {
                        “xyz1" : 0,
                        “xyz2" : 0,
                        “xyz3" : 997
                    },
                    “xyz4" : "Travis Porter Jr .",
                    “xyz5" : "AyoTravo"
                }
            ]
        }, 
        {
            "type" : 4,
            "users" : [ 
                {
                    "text" : “while",
                    "stats" : {
                        “xyz1" : 1,
                        “xyz2" : 1,
                        “xyz3" : 1200
                    },
                    “xyz4" : "LEGO Darth Vader",
                    “xyz5" : "LegoDarthVader"
                }, 
                {
                    "text" : “xjw",
                    "stats" : {
                        “xyz1" : 1,
                        “xyz2" : 1,
                        “xyz3" : 1198
                    },
                    “xyz4" : "The Brothers Brick",
                    “xyz5" : "BrothersBrick"
                }, 
                {
                    "text" : “pol",
                    "stats" : {
                        “xyz1" : 1,
                        “xyz2" : 1,
                        “xyz3" : 1197
                    },
                    “xyz4" : "BYTES & BRICKS",
                    “xyz5" : "lego_bb"
                }
            ]
        }, 
        {
            "type" : 5,
            "users" : [ 
                {
                    "text" : “qtwqyw",
                    "stats" : {
                        “xyz1" : 1,
                        “xyz2" : 1,
                        “xyz3" : 1155
                    },
                    “xyz4" : "Kell_1976",
                    “xyz5" : "LuvsMyMunchkie"
                }, 
                {
                    "text" : “ytyty",
                    "stats" : {
                        “xyz1" : 12,
                        “xyz2" : 4,
                        “xyz3" : 1200
                    },
                    “xyz4" : "carriewildes",
                    “xyz5" : "carriewildes"
                }, 
                {
                    "text" : "from the high.",
                    "stats" : {
                        “xyz1" : 0,
                        “xyz2" : 0,
                        “xyz3" : 1067
                    },
                    “xyz4" : "jake☄",
                    “xyz5" : "w0rshiptheking"
                }
            ]
        }
    ]
}

有人能帮我解决这个问题吗?

您应用运算符的方式是错误的,因为它只作用于数组字段,而您应用于非数组字段。您可以运行以下聚合管道来反规范化数组字段并获得所需的结构:

db.getCollection('topic_stats_2').aggregate([
    { "$unwind": "$usages" },
    { "$unwind": "$usages.users" },
    {
        "$project": {
            "count": 1,
            "xyz4": "$usages.users.xyz4",
            "xyz5": "$usages.users.xyz5",
            "xyz1": "$usages.users.stats.xyz1",
            "xyz2": "$usages.users.stats.xyz2",
            "xyz3": "$usages.users.stats.xyz3",
            "text": "$usages.users.text",
            "type": "$usages.type"
        }
    }
])

通过快速查看,您的$unwind似乎可疑:它们不用于数组字段。@joao:您能告诉我如何使用unwind吗?或者非常正确的回答问题。我认为unwind也可以应用于数组的属性。现在我明白了