mongoDB エラー「ReferenceError: xxx is not defined (Are you trying to run a script written for the legacy shell? Try running snippet install mongocompat)」が発生した場合の対処法

mongoDB エラー「ReferenceError: xxx is not defined (Are you trying to run a script written for the legacy shell? Try running snippet install mongocompat)」が発生した場合の対処法

mongoDBで、エラー「ReferenceError: xxx is not defined (Are you trying to run a script written for the legacy shell? Try running snippet install mongocompat)」が発生した場合の対処法を記述してます。mongoDB6からコマンドが変わったことが原因です。

環境

  • OS CentOS Stream release 9
  • MongoDB 6.0.2

エラー内容

「ls()」を使用した際に発生しました。

> ls()
ReferenceError: ls is not defined (Are you trying to run a script written for the legacy shell? Try running `snippet install mongocompat`)

原因

mongoDB6では「fs.readdirSync(path)」を使用します。

※詳細はこちらの公式ドキュメントをご確認ください

fs.readdirSync('path')

対処法

「fs.readdirSync()」を使用する

> fs.readdirSync('/')
[
  '.autorelabel', 'afs',
  'bin',          'boot',
  'config',       'data',
  'dev',          'etc',
  'home',         'lib',
  'lib64',        'logs',
  'media',        'mnt',
  'opt',          'proc',
  'root',         'run',
  'sbin',         'snap',
  'srv',          'sys',
  'tmp',          'usr',
  'var'
]