【问题标题】:aggregate returns empty array - mongoose聚合返回空数组 - 猫鼬
【发布时间】:2017-03-30 21:43:41
【问题描述】:

我有以下两个简单的查询:

Comment.aggregate([{$match: { _id: req.params.id }}])
  .exec(function(err, result) {
    // result is empty
  });

Comment.find({ _id: req.params.id })
  .exec(function (err, result) {
    // correct result returned
  });

我的问题是,聚合函数返回一个空数组。他们不应该返回相同的结果吗?

【问题讨论】:

    标签: node.js mongoose aggregate mongoose-schema


    【解决方案1】:

    是的,但是您需要将 id(它是一个字符串)转换为 objectID :)

    let idToSearch = mongoose.Types.ObjectId(req.params.id)
    Comment.aggregate([{$match: { _id: idToSearch }}])
    .exec(function(err, result) {
        // result is now correct :)
    });
    

    【讨论】:

    • 你救了我的命 :)
    猜你喜欢
    • 2015-08-04
    • 1970-01-01
    • 2018-08-22
    • 2016-04-28
    • 2020-04-26
    • 2018-08-28
    • 2022-11-22
    • 1970-01-01
    • 2021-04-14
    相关资源
    最近更新 更多