[MongoDB] MongoError: Runner error: Overflow sort stage buffered data usage of x bytes exceeds internal limit of 33554432 bytes

Express (Node.js) + Mongoose (MongoDB) なアプリケーションで下記のような MongoError: Runner error: Overflow sort stage buffered data usage of 33555427 bytes exceeds internal limit of 33554432 bytes エラーが発生しました。

MongoDB | モンゴディービー

[ERROR] default - { [MongoError: Runner error: Overflow sort stage buffered data usage of 33555427 bytes exceeds internal limit of 33554432 bytes] name: 'MongoError' }
MongoError: Runner error: Overflow sort stage buffered data usage of 33555427 bytes exceeds internal limit of 33554432 bytes
  at Object.toError (/u/apps/com/shared/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:114:11)
  at /u/apps/com/shared/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:689:54
  at Cursor.close (/u/apps/com/shared/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:972:5)
  at commandHandler (/u/apps/com/shared/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:689:21)
  at /u/apps/com/shared/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1847:9
  at Server.Base._callHandler (/u/apps/com/shared/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:445:41)
  at /u/apps/com/shared/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:478:18
  at [object Object].MongoReply.parseBody (/u/apps/com/shared/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
  at [object Object].<anonymous> (/u/apps/com/shared/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:436:20)
  at [object Object].EventEmitter.emit (events.js:95:17)
  at [object Object].<anonymous> (/u/apps/com/shared/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:201:13)
  at [object Object].EventEmitter.emit (events.js:98:17)
  at Socket.<anonymous> (/u/apps/com/shared/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:439:22)
  at Socket.EventEmitter.emit (events.js:95:17)
  at Socket.<anonymous> (_stream_readable.js:746:14)
  at Socket.EventEmitter.emit (events.js:92:17)
  at emitReadable_ (_stream_readable.js:408:10)
  at emitReadable (_stream_readable.js:404:5)
  at readableAddChunk (_stream_readable.js:165:9)
  at Socket.Readable.push (_stream_readable.js:127:10)
  at TCP.onread (net.js:528:21)

sort に指定している field に index を張ってないのが原因でした。

Mongoose の schema 定義に下記のような感じで index 張れば対応完了です。

var User = new Schema({
  created_at: {
    type: Date,
    index: 1
  }
});
 
// or
 
User.index({
  created_at: 1
});

MongoDB に直接 index 張る場合は ensureIndex 使いましょう。

db.users.ensureIndex( { created_at : 1 } )

以上です。