node.js - MongoDB/ Mongoose : find(query) and aggregate([ $match: query ]) results differ when using dates and $gte/$lte

标签 node.js mongodb mongoose

我使用 Mongoose 构建聚合管道并且匹配工作正常,直到我想使用 $gte$lte 运算符匹配日期。奇怪的是,如果我在常规 find() 中使用匹配查询,它会按预期工作:

var query = {
  dueDate: {
    $gte: moment().toISOString() // I've also tried using $date { ... }
  }
};

// finds entries matching the query [..., ...]
Model.find(query, callback);

但是,使用 $match 和相同查询进行聚合不会:

var aggregation = [{
  $match: query
}];

// finds no entries (using the same query) []
Model.aggregate(aggregation, callback);

知道为什么会这样吗?

非常感谢!

最佳答案

在使用聚合时删除 toISOString() 聚合不适用于此

这个有效:

var query = {
  dueDate: {
    $gte: moment()
  }
};

关于node.js - MongoDB/ Mongoose : find(query) and aggregate([ $match: query ]) results differ when using dates and $gte/$lte,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26925671/

相关文章:

javascript - Meteor.wrapAsync

javascript - 如何用 mongoose 存储 javascript 错误对象?

java - 在 Morphia 中使用 transient

node.js - Mongoose - 链接到同一类型的嵌套子文档

java - openssl 的 "aes-256-cfb"的 Java 名称是什么?

javascript - 如何在导入语句中使用模板文字?

javascript - 如何在每个具有多个 vCPU 的多个 Google App Engine 实例之间共享缓存?

mongodb - Mongoid 按值或默认值查询

Mongoose Deep Populate 限制中间模型

node.js - Mongoose "update"不起作用,除非我使用 ".then"