【问题标题】:MongoDB aggregate + $match + $group + ArrayMongoDB 聚合 + $match + $group + 数组
【发布时间】:2019-03-27 16:51:51
【问题描述】:

这是我的 MongoDB 查询:

profiles.aggregate([{"$match":{"channels.sign_up":true}},{"$group":{"_id":"$channels.slug","user_count":{"$sum":1}}},{"$sort":{"user_count":-1}}])

这是我的代码:

$profiles = Profile::raw()->aggregate([
            [
                '$match' => [
                    'channels.sign_up' => true
                ]
            ],
            [
                '$group' => [
                    '_id' => '$channels.slug',
                    'user_count' => ['$sum' => 1]
                ]
            ],
            [
                '$sort' => [
                    "user_count" => -1
                ]
            ]
        ]);

这是我的 Mongo 收藏:

"channels": [
        {
            "id": "5ae44c1c2b807b3d1c0038e5",
            "slug": "swachhata-citizen-android",
            "mac_address": "A3:72:5E:DC:0E:D1",
            "sign_up": true,
            "settings": {
                "email_notifications_preferred": true,
                "sms_notifications_preferred": true,
                "push_notifications_preferred": true
            },
            "device_token": "ff949faeca60b0f0ff949faeca60b0f0"
        },
        {
            "id": "5ae44c1c2b807b3d1c0038f3",
            "slug": "website",
            "mac_address": null,
            "device_token": null,
            "created_at": "2018-06-19 19:15:13",
            "last_login_at": "2018-06-19 19:15:13",
            "last_login_ip": "127.0.0.1",
            "last_login_user_agent": "PostmanRuntime/7.1.5"
        }
],

这是我的回复:

   {
        "data": [
            {
                "_id": [
                    "swachhata-citizen-android"
                ],
                "user_count": 1
            },
            {
                "_id": [
                    "icmyc-portal"
                ],
                "user_count": 1
            },
            {
                "_id": [
                    "swachhata-citizen-android",
                    "website",
                    "icmyc-portal"
                ],
                "user_count": 1
            }
        ]
    }

我期待的是:

{
    "data": [
        {
            "_id": [
                "swachhata-citizen-android"
            ],
            "user_count": 1
        },
        {
            "_id": [
                "icmyc-portal"
            ],
            "user_count": 1
        },
        {
            "_id": [
                "website",
            ],
            "user_count": 1
        }
    ]
}

如您所见,channels 是一个数组,“sign_up”仅适用于用户注册的数组中的一个元素,因为我们有很多应用程序,因此我们必须为用户维护多个通道。

我想记录有多少用户注册了不同的频道,但作为响应,它来自所有频道,而不是一个 sign_up 为真的频道。

计数也是错误的,因为我必须记录 "slug": "swachhata-citizen-android" 和 "sign_up": true 的位置。

需要建议:)

【问题讨论】:

  • user_count 包含什么?

标签: mongodb mongodb-query jenssegers-mongodb


【解决方案1】:

使用$unwind 将每个包含数组的文档转换为包含嵌套字段的文档数组。在您的示例中,如下所示:

profiles.aggregate([
  {$unwind: '$channels'},
  {$match: {'channels.sign_up': true}},
  {$group: {_id: '$channels.slug', user_count: {$sum: 1}}},
  {$sort: {user_count: -1}}
])

【讨论】:

  • 另外,如果你能帮我弄清楚添加多个过滤器的方法,比如如果 city_id 被传递,那么我必须与之匹配,如果 state_id 已通过,我必须匹配 state_id,如果日期范围已通过,则我必须在给定的日期范围内检查。我正在使用 jenssegers/laravel-mongodb
  • 我很确定你可以独立于你的包来完成它。只需逐步构建您的查询 (JS) var match = {}; if (city_id) match.city_id = city_id; /* ... */.
  • 我在 php 中尝试过,但最后给出错误“MongoDB\Driver\Exception\CommandException:匹配过滤器必须是“if (request()->input('city_id')) { $matchCon = [ "city_id" => request()->input('city_id'), ]; } else { $matchCon = [ "city_id_fake_field" => request()->input('city_id'), ]; } $object = json_decode(json_encode($matchCon), FALSE); 中的对象中的表达式,而不是我传递的@987654326 @在$match条件中
  • 我对这个工具一点也不熟悉,抱歉。
猜你喜欢
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 2013-03-10
  • 1970-01-01
相关资源
最近更新 更多