不要直接对比字符串,应该将时间字段转换成字符串类型再进行对比。
比如:
db.collection(“rideRecords”)
.aggregate()
.match({
‘record.subLineId’: ‘l1’,
__ creationDate: _.gte(‘2022-01-30 00:00:00’).and(_.lte(‘2022-01-30 23:59:59’)),__
‘record.people._id’: “381d149061ac0a5a00921a680d1281fe”
})
.lookup({
from: “sublineRecords”,
localField: “_id”,
foreignField: “rideRecords”,
as: “sublineRecords”
})
.lookup({
from: “driverRecords”,
localField: “sublineRecords.driverRecordID”,
foreignField: “_id”,
as: “driverRecords”
})
.end()
上面语句,执行时返回空。
改成:
db.collection(“rideRecords”)
.aggregate()
.addFields({
formatDate: $.dateToString({
date:’$creationDate’,
format:’%Y-%m-%d %H:%M:%S’,
timezone:‘Asia/Shanghai’
})
})
.match({
‘record.subLineId’: ‘s4’,
formatDate:_.gte(‘2022-01-30 00:00:00’).and(_.lte(‘2022-01-30 23:59:59’)),
})
.end()
关键点:把日期型通过格式转化:dateToString,转成字符类型再做比较
dateToString 相关文档: