【发布时间】:2021-12-24 15:54:24
【问题描述】:
我编写了一个 MongoDB 查询来获取在给定日期(匹配年份和月份)关闭的订单:
const orders = await OrderModel.find({
$expr: {
$and: [
{ $eq: ['$entityId', entityId] },
{ $ne: ['$closingDate', null] },
{ $eq: [{ $year: '$closingDate' }, date.getFullYear()] },
{ $eq: [{ $month: '$closingDate' }, date.getMonth() + 1] },
],
},
}).lean();
原来我的本地 MongoDB 版本 (3.6) 高于开发环境中的版本 (3.4.14),因此不支持 $expr。我可以使用旧版本的替代品吗?
注意:我阅读了有关此主题的 other thread,但我无法从中提取答案,因为它涵盖了不同的用例。
【问题讨论】:
-
你试过避开
$expr吗?喜欢this example -
@J.F.是的,避免它是我想要做的!我只是不知道这个特定的用例 - 使用 $month 和 $year 运算符。标准的 .find() 查询是否支持它们?
-
嗯,没错,您正在使用
$year和$month。所以也许你可以使用aggregate和$match而不是find。 -
我将查询更改为查找
closingDate: { $ne: null, $gte: current, $lt: next },它似乎正在工作。谢谢! -
请不要粘贴截图,使用格式化文本。见meta.stackoverflow.com/q/285551/3027266
标签: database mongodb operators version compatibility