【问题标题】:MongoDB Aggregation Framework: Getting $unwind error when using $groupMongoDB 聚合框架:使用 $group 时出现 $unwind 错误
【发布时间】:2013-02-27 00:04:14
【问题描述】:

我的文档结构如下:

{
  "_id" : NumberLong("80000000012"),
[...]
  "categories" : [{
      "parent" : "MANUFACTURER",
      "category" : "Chevrolet"
    }, {
      "parent" : "MISCELLANEOUS",
      "category" : "Miscellaneous"
    }],
[...]
}

我正在尝试为每个“父”字段获取所有“类别”字段的不同列表。我试图利用聚合框架通过以下查询来执行此操作:

db.posts_temp.aggregate(
    {$unwind : '$categories'},
    {$match : {'categories.parent' : 'MISCELLANEOUS'}},
    {$project : {
        '_id' : 0,
        parent : '$categories.parent',
        category : '$categories.category'
        }
    },
    {
        $group : { 
            _id : '$parent',
            category : {$addToSet : '$category'}
        }
    }
);

运行此查询会返回以下错误:

{
    "errmsg" : "exception: $unwind:  value at end of field path must be an array",
    "code" : 15978,
    "ok" : 0
}

这似乎与查询的组部分相关联,因为当我删除它时,查询会正确运行,但很明显,数据不在我想要的位置。

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    我刚刚尝试在我的 mongo 实例上执行上述聚合查询。这是我的 3 个文档,每个文档都有一个类别键,其中包含两个嵌套文档的数组。

    这是我的数据:

    {
        "_id" : ObjectId("512d5252b748191fefbd4698"),
        "categories" : [
            {
                "parent" : "MANUFACTURER",
                "category" : "Chevrolet"
            },
            {
                "parent" : "MISCELLANEOUS",
                "category" : "Miscellaneous"
            }
        ]
    }
    {
        "_id" : ObjectId("512d535cb748191fefbd4699"),
        "categories" : [
            {
                "parent" : "MANUFACTURER",
                "category" : "Chevrolet"
            },
            {
                "parent" : "MISCELLANEOUS",
                "category" : "Pickup"
            }
        ]
    }
    {
        "_id" : ObjectId("512d536eb748191fefbd469a"),
        "categories" : [
            {
                "parent" : "MANUFACTURER",
                "category" : "Toyota"
            },
            {
                "parent" : "MISCELLANEOUS",
                "category" : "Miscellaneous"
            }
        ]
    }
    

    这是我运行的您的聚合查询:

    db.posts_temp.aggregate( {$unwind:'$categories'} , {$match: {'categories.parent':'MISCELLANEOUS'}}, {$project:{'_id':0, parent: '$categories.parent', category:'$categories.category'}}, {$group:{_id:'$parent', category:{$addToSet:'$category'}}})
    

    结果如下:

    {
        "result" : [
            {
                "_id" : "MISCELLANEOUS",
                "category" : [
                    "Pickup",
                    "Miscellaneous"
                ]
            }
        ],
        "ok" : 1
    }
    

    如果我的数据与您的数据之间存在差异,请告诉我。

    CSharpie

    【讨论】:

    • 我们在工作中再次运行了这个,并将 match 语句移到了 unwind 之前并修复了它。我们最好的猜测是展开创建了某种太大的内存集合并引发了错误。我们在集合中有大约 18k 文档。 耸耸肩
    • ^他说的。感谢收看。
    猜你喜欢
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2012-06-26
    • 1970-01-01
    相关资源
    最近更新 更多