mongodb:数组的计数和排序内容
我有一个包含以下结构的集合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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需
$ undind
通过复合组键而来的密码数组和组。这是 mongo Playground 供您参考。
Simply
$unwind
the passwords array and group by composite group key.Here is the Mongo playground for your reference.