我有一个 MongDB 集合,其中我的文档有一个存储为字符串的日期,如下所示:
"data": {
"type": "MatchData",
"matchId": "5b34f2d527f0d904f8ee4bbf",
"groupId": "5b2a032ffc37de04f30dc412",
"leaderboardId": "5b2a0387fc37de04f30e149d",
"matchTitle": "",
"date": "20180628144304"
}
如您所见,日期保存为字符串,但我希望能够找到所有日期早于当前日期的记录:
var currDate = 20180928000000
dbCollection('matches').find( { "groupId": groupId, "$lt": {"date": currDate} } );
当前示例无法正常工作,因为 currDate 是一个整数,而来自文档的日期是一个字符串!?我如何在查询中使用 parseInt 还是有其他方法?
希望得到帮助并提前致谢:-)
最佳答案
您可以将输入参数 currDate
转换为字符串
var currDate = "20180928000000"
dbCollection('matches').find({ "groupId": groupId, "$lt": { "date": currDate } })
或者如果您使用的是 4.0
然后你可以使用$toInt
聚合算子
dbCollection('matches').find({
"groupId": groupId,
"$expr": { "$lt": [{ "$toInt": "$date" }, currDate]}
})
关于javascript - MongoDB 在调用字符串中比较 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52581374/