【问题标题】:Mongodb error : The 'cursor' option is required, except for aggregation explainMongodb错误:需要'cursor'选项,聚合说明除外
【发布时间】:2017-10-12 20:22:33
【问题描述】:

我正在使用 mongodb 3.5.5 和 mongoose 4.9.8 并且 Node.js 版本是 7.10,当我将我的应用程序发布到生产服务器时,发生了错误,但在我的开发环境中工作。

我该如何解决?

错误信息:

{ MongoError: The 'cursor' option is required, except for aggregation explain
     at Function.MongoError.create (/data/deploy/aaa/webapp/node_modules/mongodb-core/lib/error.js:31:11)
     at /data/deploy/aaa/webapp/node_modules/mongodb-core/lib/connection/pool.js:489:72
     at authenticateStragglers (/data/deploy/aaa/webapp/node_modules/mongodb-core/lib/connection/pool.js:435:16)
     at Connection.messageHandler (/data/deploy/aaa/webapp/node_modules/mongodb-core/lib/connection/pool.js:469:5)
     at Socket.<anonymous> (/data/deploy/aaa/webapp/node_modules/mongodb-core/lib/connection/connection.js:321:22)
     at emitOne (events.js:96:13)
     at Socket.emit (events.js:191:7)
     at readableAddChunk (_stream_readable.js:178:18)
     at Socket.Readable.push (_stream_readable.js:136:10)
     at TCP.onread (net.js:561:20)
   name: 'MongoError',
   message: 'The \'cursor\' option is required, except for aggregation explain',
   ok: 0,
   errmsg: 'The \'cursor\' option is required, except for aggregation explain',
   code: 9,
   codeName: 'FailedToParse' }

js代码:

  articleLikeSchema.statics.sumById = function ({id = ''} = {}) {
    return this.model('ArticleLike').aggregate([
      { $match: { id: id } },
      { $group: { _id: '$id', count: { $sum: 1 } } }
    ]).then(sum => {
      if (!sum || sum.length === 0) return Promise.resolve({count: 0})
      else return Promise.resolve(sum[0])
    })
  }

Mongoose 执行命令:

Mongoose: articlelikes.aggregate([ { '$match': { id: '1494606935' } }, { '$group': { _id: '$id', count: { '$sum': 1 } } } ], {})

【问题讨论】:

标签: node.js mongodb mongoose


【解决方案1】:

您需要为 Mongo 3.6 中更改的聚合调用提供光标选项

https://docs.mongodb.com/manual/reference/command/aggregate/#dbcmd.aggregate

所以在聚合调用中添加 {cursor:{}} 应该可以解决这个问题:

  articleLikeSchema.statics.sumById = function ({id = ''} = {}) {
    return this.model('ArticleLike').aggregate([
          { $match: { 
                id: id 
              } 
          },
          { $group: { 
                _id: '$id', 
                count: { $sum: 1 } 
              } 
          }
        ], 
        { cursor:{} }
    ).then(sum => {
      if (!sum || sum.length === 0) return Promise.resolve({count: 0})
      else return Promise.resolve(sum[0])
    })
  }

【讨论】:

  • 我尝试使用 Mongo 3.5 并失败并出现同样的错误。 3.4 成功了。
  • @iurisilvio - 你的意思是 Mongo 3.6 吗?
  • 不,@Himanshu 3.5。没调查,因为生产的mongo是3.4,我只是换了本地版本。
  • @iurisilvio - 稳定版本是 3.4 或 3.6。不知道你为什么选择3.5。上面的更改是针对 3.6 的,而 3.5 可能不支持?
【解决方案2】:

使用 Mongoose 3.6、Mongoose 4.13.20、Node 6.11.0 +“回调方法”,我必须这样做:

return this.model('ArticleLike').aggregate([
      { $match: { id: id } },
      { $group: { _id: '$id', count: { $sum: 1 } } }
    ]).exec(function (err, results) {
      // ...
    })

无需添加包含{ cursor: {} }的参数。

【讨论】:

    猜你喜欢
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多