运行以下查询时,结果将显示在mongo shell中。
样本记录

{
    "_id" : ObjectId("587e21df6e79d255011a9c6a"),
    "vendor_id" : "101",
    "subscription_id" : 14,
    "created_at" : ISODate("2017-01-17T13:53:35.272Z")
}
{
    "_id" : ObjectId("587e21df6e79d255011a9c6c"),
    "vendor_id" : "102",
    "subscription_id" : 14,
    "created_at" : ISODate("2017-01-17T13:56:35.272Z")
}

查询
db.user_config.aggregate({$group:
{
    _id : "$subscription_id",
    list:
    {
        $push:
        {
            _id: "$_id",
            vendor_id: "$vendor_id"
        }
     }
}})

结果
/* 1 */
{
    "result" : [
        {
            "_id" : 14,
            "list" : [
                {
                    "_id" : ObjectId("587e21df6e79d255011a9c6a"),
                    "vendor_id" : "user_101"
                },
                {
                    "_id" : ObjectId("587e21df6e79d255011a9c6b"),
                    "vendor_id" : "user_101"
                }
            ]
        }
    ],
    "ok" : 1.0000000000000000
}

但当通过laravel执行相同的操作时,会发生以下错误。$pipeline不是列表(意外索引:“$group”)
下面是拉威尔密码
$configuration_list = UserConfig::raw()->aggregate([
            '$group' => [
                    '_id'    => '$subscription_id',
                    'list'  => ['$push' =>
                     ['_id' => '$_id', 'vendor_id' => '$vendor_id']]
                ]
            ]
        );

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

最佳答案

Wrap the aggregation pipeline steps in an array

$pipeline = [
    [
        '$group' => [
            '_id' => '$subscription_id',
            'list' => [
                '$push' => [
                    '_id' => '$_id',
                    'vendor_id' => '$vendor_id'
                ]
            ]
        ]
    ]
];
$configuration_list = UserConfig::raw()->aggregate($pipeline);

关于php - $ pipeline不是Laravel中的列表(意外索引:“$ group”),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41704918/

10-13 00:53