Mongodb的排序函數sort()排序方式為:數字、中文首字母的順序 升序(a-z)或者降序(z-a) 進行升序或者降序
db.getCollection('vuln').find({},{"_id":0,"severity":1,"vulnerability_id":1}).sort({"vulnerability_id":1})
但是部分類別為單詞,例如,low,medium,high 根據這個排序,簡單的辦法,錄入的時候使用int來標記對應類別,但是如果整改比較麻煩,或者其他接口已經使用,也可以使用聚合查詢
db.vuln.aggregate( [ { "$match": { "$and": [ { "domain": "test.a.com" } ], "is_delete": false } }, { "$project": { "category": 1, "severity": { "$switch": { "default": 0, "branches": [ { "case": { "$eq": [ "$severity", "low" ] }, "then": 1 }, { "case": { "$eq": [ "$severity", "medium" ] }, "then": 2 }, { "case": { "$eq": [ "$severity", "high" ] }, "then": 3 }, { "case": { "$eq": [ "$severity", "critical" ] }, "then": 4 } ] } }, "is_delete": 1, "fix_status": 1, "title": 1, "definiteness": 1, "hash_id": 1 } }, { "$limit": 10 }, { "$skip": 0 }, { "$sort": { "severity": -1 } } ] )