在聚合中使用$ unwind运算符时出现mongodb错误

时间:2014-02-12 11:05:28

标签: mongodb exception aggregation-framework

这是我的收藏文件

[0] => Array
    (
        [Patent] => Array
            (
                [allocation] => Array
                    (
                        [1] => Array
                            (
                                [r_1_a] => 17
                            )

                    )

                [pn] => US20120101874A1
                [id] => 52fb18775f44eaaf0a8b462a
            )

    )

我正在尝试对此查询使用$ unwind运算符:

db.patents.aggregate([{'$unwind':'$allocation'}]);

这给了我例外:

Error: Printing Stack Trace
at printStackTrace (src/mongo/shell/utils.js:37:15)
at DBCollection.aggregate (src/mongo/shell/collection.js:897:9)
at (shell):1:12
Wed Feb 12 16:31:09.515 aggregate failed: {
"errmsg" : "exception: $unwind:  value at end of field path must be an array",
"code" : 15978,
"ok" : 0
} at src/mongo/shell/collection.js:898

帮助我解决这个问题,因为我在数组上应用$ unwind。

1 个答案:

答案 0 :(得分:0)

您必须展开每个嵌套数组级别:

db.patents.aggregate([
    {'$unwind':'$Patent'},
    {'$unwind':'$Patent.allocation'}
]);

修改

查看到目前为止发布的内容,错误原因是allocation对象而不是数组。最终这是mongo告诉你的:

"errmsg" : "exception: $unwind:  value at end of field path must be an array"