javascript - MongoDB 在调用字符串中与 int 进行比较
问题描述
我有一个 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]}
})
推荐阅读
- mysql - NestJS/TypeORM。TypeORM 不更新数据库中的实体,而是使用旧的缓存实体
- c++ - const 在函数参数中的作用是什么,为什么我们在比较函数中取向量的地址?
- vuetify.js - Vuetify - 小屏幕上缺少导航图标
- java - Hadoop bzip2 拆分偏移量
- php - 识别主题标签 # 或 $ 并自动将文本替换为链接
- spring - 覆盖 Spring Boot 配置不起作用
- r - 为什么 mapboxer_proxy 不能在模块化闪亮应用程序中工作?
- android - 只有 android.R.transition.move 不起作用
- mysql - 如何修复此示例的 SQL 语法?
- python - 分割图像中没有边缘(线性梯度)的单调区域