MongoDB查询排序出现异常,请各位大大指点一下 #1744
Unanswered
BonneauYang
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
由于服务器排行榜处理大数的需要,我将排行榜字段存储为字符串。数据库版本为MongoDB 3.6. 在服务器启动时,需要初始化排行榜数据,这里只排前100。我的想法是先将排行榜字段按小数点分割为整数部分和小数部分,然后对比整数部分字符串的长度,长度越大则排行靠前,长度一致时对比字符串,按字典顺序即可。整数部分一致的话,则对比小数部分字符串,直接对比字符串即可。此逻辑我也在MongoDB Compass里验证了,确实可行。
测试数据如下:
[{"level":"50","role_name":"游客6957681","rid":6957681}}, {"level":"3948","role_name":"游客7624305","rid":7624305}, {"level":"98.5","role_name":"游客3430001","rid":3430001}, {"level":"100.0", "role_name":"test","rid":3287665}, {"level":"100","role_name":"游客7481969","rid":7481969}, {"level":"98","role_name":"游客2905713","rid":2905713}]
MongoDB Compass相关操作如下:
[ { $addFields: { integerPart: { $cond: { if: { $gte: [ { $indexOfCP: [ "$level", ".", ], }, 0, ], }, then: { $substrCP: [ "$level", 0, { $indexOfCP: [ "$level", ".", ], }, ], }, else: "$level", }, }, decimalPart: { $cond: { if: { $gte: [ { $indexOfCP: [ "$level", ".", ], }, 0, ], }, then: { $substrCP: [ "$level", { $add: [ { $indexOfCP: [ "$level", ".", ], }, 1, ], }, { $subtract: [ { $strLenCP: "$level", }, { $add: [ { $indexOfCP: [ "$level", ".", ], }, 1, ], }, ], }, ], }, else: "", }, }, integerPartLength: { $cond: { if: { $gte: [ { $indexOfCP: [ "$level", ".", ], }, 0, ], }, then: { $strLenCP: { $substrCP: [ "$level", 0, { $indexOfCP: [ "$level", ".", ], }, ], }, }, else: { $strLenCP: "$level", }, }, }, }, }, { $sort: { integerPartLength: -1, integerPart: -1, decimalPart: -1, }, }, ]
验证结果如下:
[{"level":"3948","role_name":"游客7624305","rid":7624305,"integerPart":"3948","decimalPart":"","integerPartLength":4}, {"level":"100.0", "role_name":"test","integerPart":"100","rid":3287665,"decimalPart":"0","integerPartLength":3}, {"level":"100","role_name":"游客7481969","rid":7481969,"integerPart":"100","decimalPart":"","integerPartLength":3},{"level":"98.5","role_name":"游客3430001","rid":3430001,"integerPart":"98","decimalPart":"5","integerPartLength":2}, {"level":"98","role_name":"游客2905713","rid":2905713,"integerPart":"98","decimalPart":"","integerPartLength":2}, {"level":"50","role_name":"游客6957681","rid":6957681,"integerPart":"50","decimalPart":"","integerPartLength":2}]
lua代码如下:
`
---初始化排行榜
---@param rankProperty string @排序属性
---@param rankSkipQuantity number @排行榜跳过数量
---@param rankLimitQuantity number @排行榜限制数量
---@return PlayerPublicInfo[], number, number @排行榜信息数据列表, 全部排行数量, 查询排行数量
function CenterServerMongodbManager:initRank(rankProperty, rankSkipQuantity, rankLimitQuantity)
---@type table @mongodb配置
local mongodbsConfig = configdao.get_svrs_config("mongo_dbs")
end
最终出现错误查询数据如下:
{{"level":"98.5","role_name":"游客3430001","rid":3430001,"integerPart":"98","decimalPart":"5","integerPartLength":2}, {"level":"100.0", "role_name":"test","integerPart":"100","rid":3287665,"decimalPart":"0","integerPartLength":3}, {"level":"3948","role_name":"游客7624305","rid":7624305,"integerPart":"3948","decimalPart":"","integerPartLength":4}, {"level":"100","role_name":"游客7481969","rid":7481969,"integerPart":"100","decimalPart":"","integerPartLength":3}, {"level":"98","role_name":"游客2905713","rid":2905713,"integerPart":"98","decimalPart":"","integerPartLength":2}, {"level":"50","role_name":"游客6957681","rid":6957681,"integerPart":"50","decimalPart":"","integerPartLength":2}}`请问各位大大,为什么会出现这种情况吖?是lua的原因么
Beta Was this translation helpful? Give feedback.
All reactions