mongodb:数组的计数和排序内容

发布于 2025-01-23 21:35:25 字数 1174 浏览 0 评论 0原文

我有一个包含以下结构的集合loginAttEmptList:

    {
        "username":  "root",
        "passwords":  [
                          "1234",
                          "root",
                          "123456"
                      ],
    },
    {
            "username":  "admin",
            "passwords":  [
                              "1234",
                              "123456",
                              "admin",
                              "administrator"
                          ],
        },
        {
            "username":  "root",
            "passwords":  [
                              "1234",
                              "root"
                          ],
        }

我能够计数并对最常用的用户名进行排序:

    db.loginAttemptList.aggregate([
        {$group : {_id:"$username", count:{$sum:1}}},
        {$sort: {count:-1}}
    ])

现在我想对密码进行相同的操作。每个对象中的数组中都有0-10个密码(给定)。是否有可能对它们进行计数和分类?

像:

“ 1234”:3276(times)

“ 123456”:2345

“密码”:1349

等。

还有其他方法可以列出最常用的用户名/密码组合吗?

I have a collection loginAttemptList with the following structure:

    {
        "username":  "root",
        "passwords":  [
                          "1234",
                          "root",
                          "123456"
                      ],
    },
    {
            "username":  "admin",
            "passwords":  [
                              "1234",
                              "123456",
                              "admin",
                              "administrator"
                          ],
        },
        {
            "username":  "root",
            "passwords":  [
                              "1234",
                              "root"
                          ],
        }

I am able to count and sort the most used usernames:

    db.loginAttemptList.aggregate([
        {$group : {_id:"$username", count:{$sum:1}}},
        {$sort: {count:-1}}
    ])

Now I would like to do the same with passwords. There are 0-10 passwords in an array in every object (given). Is there a possibility to count and sort them as well?

Something like:

"1234" : 3276 (times)

"123456": 2345

"password": 1349

etc.

And further is there a way to list the most used username/password combination?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人接听 2025-01-30 21:35:25

只需$ undind通过复合组键而来的密码数组和组。

db.collection.aggregate([
  {
    "$unwind": "$passwords"
  },
  {
    $group: {
      _id: {
        username: "$username",
        password: "$passwords"
      },
      count: {
        $sum: 1
      }
    }
  },
  {
    $sort: {
      count: -1
    }
  }
])

这是 mongo Playground 供您参考。

Simply $unwind the passwords array and group by composite group key.

db.collection.aggregate([
  {
    "$unwind": "$passwords"
  },
  {
    $group: {
      _id: {
        username: "$username",
        password: "$passwords"
      },
      count: {
        $sum: 1
      }
    }
  },
  {
    $sort: {
      count: -1
    }
  }
])

Here is the Mongo playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文