【问题标题】:Mongoose aggregation not working猫鼬聚合不起作用
【发布时间】:2012-11-15 07:13:14
【问题描述】:

Mongodb 2.2.0 和mongoose 3.3.1。

我正在尝试在以下查询中运行 aggregate

var db = mongoose.connect('localhost', 'test', '27017').connection;
var testSchema = new Schema;
var tr= db.model('tests', testSchema);  

tr.aggregate({'$group':{'_id':'$test_id', 'average':{$avg:'$tes_tscore'}}})

以下错误不断弹出:

TypeError: Object function model(doc, fields, skipId) {
    if (!(this instanceof model))
      return new model(doc, fields, skipId);
    Model.call(this, doc, fields, skipId);
  } has no method 'aggregate'  

mongoose 还不支持聚合还是我的代码遗漏了什么?

** tests 集合有超过 1000 个文档。运行 tr.find(... console.log(...) 返回所需的文档。

【问题讨论】:

  • 您可以使用 db.runCommand("aggregate":collectionName, "pipeline":) 在不使用聚合助手的情况下进行聚合

标签: mongodb mongoose


【解决方案1】:

我解决了。

在脚本中输入 console.log(mongoose.version); 会显示为过时版本 (3.0.2)。

我重新浏览了这些模块(这次很小心)并删除并重新安装了mongoose
版本 3.3.1 和 aggregate 现在可以使用。

aggregate 上线 v.3.1.0

【讨论】:

    【解决方案2】:

    3.x 中支持聚合。您示例中的代码看起来不错,但不知何故 tr 指的是 model() 方法而不是模型实例。如果您能够始终如一地重现此问题,请打开一张包含要运行的确切代码的工单,我们会修复它。

    https://github.com/LearnBoost/mongoose/issues/new

    【讨论】:

    • 我可以始终如一地重现此问题。不仅适用于这个模型,也适用于其他模型。我会提交一张票。
    猜你喜欢
    • 2016-05-04
    • 2013-06-24
    • 2015-05-09
    • 2015-05-19
    • 2019-12-02
    • 2015-07-27
    • 2023-03-03
    • 1970-01-01
    • 2019-08-29
    相关资源
    最近更新 更多