【问题标题】:Mongo match aggregate not working with dateMongodb匹配聚合不适用于日期
【发布时间】:2020-12-22 22:46:22
【问题描述】:

我正在使用mongoose,以下工作正常。

const TODAY = moment().format('YYYY-MM-DD');
const todaysFoos = await Foo.find({updatedAt: { $gte: TODAY }, someField: { $ne: '' } });

其中updatedAt 是使用Mongoose Model 中的 timestamps: true 创建/更新的

但是,我无法使用 aggregate 进行此操作 const mapping = { alias: "$someField" };

 const pipeline = [
    { $match: { updatedAt: { $gte: TODAY }, someField: { $ne: '' } } },
    { $project: mapping }
];

const todaysFoos = await Foo.aggregate(pipeline);
                                            

我一直没有从聚合中得到任何回报,而第一个 find 工作得很好。有什么想法吗?

【问题讨论】:

  • 查询是否返回一些没有$project 阶段的结果?
  • 不,不是。我认为这与match 有关

标签: javascript node.js mongodb mongoose aggregate


【解决方案1】:

我不得不这样做

let TODAY = moment().format('YYYY-MM-DD');
TODAY = new Date(TODAY);

我不知道 $match 需要这个,但它适用于 find。有人知道吗?

【讨论】:

    猜你喜欢
    • 2019-01-24
    • 2015-09-06
    • 1970-01-01
    • 2013-04-05
    • 2019-04-15
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多