我有一個具有以下結構的集合 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(次)
“123456”:2345
“密碼”:1349
等等
還有沒有辦法列出最常用的用戶名/密碼組合?
uj5u.com熱心網友回復:
只需$unwind密碼陣列并按復合組鍵分組。
db.collection.aggregate([
{
"$unwind": "$passwords"
},
{
$group: {
_id: {
username: "$username",
password: "$passwords"
},
count: {
$sum: 1
}
}
},
{
$sort: {
count: -1
}
}
])
這是Mongo游樂場供您參考。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/465222.html
