在 MongoDB 中,可以使用 $gte
(大于等于)与 $lt
(小于)操作符来查询今天的数据。以下是一个示例查询今天数据的代码:
const today = new Date();today.setHours(0, 0, 0, 0);const tomorrow = new Date(today);tomorrow.setDate(today.getDate() + 1);db.collection('yourCollection').find({createdAt: {$gte: today,$lt: tomorrow}});
这个查询会查找 yourCollection
集合中 createdAt
字段值在今天 00:00:00 和明天 00:00:00 之间的数据。要查询其他字段,请将 createdAt
替换为您要查询的字段名。