Javascript MongoDB在调用字符串中比较int
我有一个MongDB集合,其中我的文档将日期存储为如下字符串:Javascript MongoDB在调用字符串中比较int,javascript,mongodb,Javascript,Mongodb,我有一个MongDB集合,其中我的文档将日期存储为如下字符串: "data": { "type": "MatchData", "matchId": "5b34f2d527f0d904f8ee4bbf", "groupId": "5b2a032ffc37de04f30dc412", "leaderboardId": "5b2a0387fc37de04f30e149d", "matchTitle": "", "date": "2018062814430
"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
然后可以使用聚合运算符
dbCollection('matches').find({
"groupId": groupId,
"$expr": { "$lt": [{ "$toInt": "$date" }, currDate]}
})
愚蠢的问题,但是像
{“date”:currDate.toString()}
这样的东西能工作吗?如果我必须在mongodb中存储日期,那么。。我会将它们存储为date(),而不是string、int或其他非date的内容。您可以在较新版本(4.0)中使用$toInt
函数。类似于dbCollection('matches').find({“groupId”:groupId,“$expr”:{“$lt”:[{“$toInt”:“$date”},currDate]})
如果您使用的是4.0,则可以使用$toInt
聚合