doufan6886 2018-01-31 18:12
浏览 60

如何在jenssegers / laravel-mongodb中放松?

I am using mongodb with Laravel. I also using jenssegers/laravel-mongodb for performing database activity. I have one collection containing data of the user where the path of his picture is stored with the tags given by him. Somewhat similar to Instagram. Now I want to perform tag wise count on the data i.e. for every tag, how many records are there. Below is the structure of the collection named user_contents:

{
    "_id": ObjectId("5a6f41fbf4f1f331b4006d7d"),
    "user_id": "1",
    "file_name": "rBrnbxjJXckR1CnvljZ4eZiXFWFWBobJiguUbHzd.jpeg",
    "tags": ["blue", "girl"],
    "created_at": ISODate("2018-01-29T15:47:06Z"),
    "updated_at": ISODate("2018-01-29T15:47:06Z")
}

I am runnig the below query to get the desired data which works perfectly in a mongo client

db.user_contents.aggregate([{
"$project": {
    "tags": 1
}
}, {
"$unwind": "$tags"
}, {
"$group": {
    "_id": "$tags",
    "count": {
        "$sum": 1
    }
}
}]);

But I don't know how to write this query using jenssegers. More specifically, I am not getting an option to execute unwind. It would be great if someone can help me with this,

Version info:

Laravel: 5.5.32

MongoDB: db version v3.6.2

php: 7.1.10

Thank you.

  • 写回答

1条回答 默认 最新

  • drvjlec1767 2018-03-09 14:47
    关注

    This is typically how I go about it:

    User::raw(function ($collection) {
            return $collection->aggregate([
               ['$project' => [
                    "tags" => 1,
                     ...
                ]],
                ['$unwind' => '$tags'],
                ['$group' =>
                    [
                        '_id'=>'$tags',
                        'count' => ['$sum'=>1]
                    ]
                ],
            ]);
        })
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?