Node项目使用MongoDB,从3.2升级到3.6.0的几个坑。

0. 项目背景

  • Node.js 8.x
  • mongoose 4.11.7 -> 4.13.7
  • MongoDB 3.2.x -> 3.6.0

1. 数据库文件不兼容

使用相同的配置文件,MongoDB服务启动出错。配置文件如下:

systemLog:
destination: file
path: d:\MongoDB\log\mongo.log
logAppend: true
storage:
dbPath: d:\MongoDB\data

解决方案

具体原因未知,只好做数据迁移。

2. aggregate未指定cursor选项导致报错

报错堆栈:

error: MongoError: The 'cursor' option is required, except for aggregate with the explain argument
at Function.MongoError.create (/opt/live/source/live-server/node_modules/mongodb-core/lib/error.js:31:11)
at /opt/live/source/live-server/node_modules/mongodb-core/lib/connection/pool.js:497:72
at authenticateStragglers (/opt/live/source/live-server/node_modules/mongodb-core/lib/connection/pool.js:443:16)
at Connection.messageHandler (/opt/live/source/live-server/node_modules/mongodb-core/lib/connection/pool.js:477:5)
at Socket.<anonymous> (/opt/live/source/live-server/node_modules/mongodb-core/lib/connection/connection.js:331:22)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:594:20)

原因

从MongoDB 3.5.x开始必须在使用aggregate时必须指定cursor选项。

参考:

解决方案

如果是mongoose项目升级到4.13.7。

3. Unknown modifier $pushAll

报错堆栈

MongoError: Unknown modifier: $pushAll
at Function.MongoError.create (/opt/live/source/live-server/node_modules/mongodb-core/lib/error.js:31:11)
at toError (/opt/live/source/live-server/node_modules/mongodb/lib/utils.js:139:22)
at /opt/live/source/live-server/node_modules/mongodb/lib/collection.js:1060:67
at /opt/live/source/live-server/node_modules/mongodb-core/lib/connection/pool.js:469:18
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)
You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:

原因

从 MongoDB 3.6开始,此方法被移除了。It is trying to use $pushAll, which is deprecated since 2.4 and it is not in MongoDB 3.6.

解决方案

如果使用Mongoose 4.x,创建Scheme时,指定usePushEach选项。或者升级到Mongoose 5.x(此时还未发布)。

new Schema(obj, { usePushEach: true });